473,513 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

chop() and empty() functions

I've been using the following lambda/function for a number of months now
(I got the idea from someone in #python, though I don't remember who):

def chop(s, n):
"""Chops a sequence, s, into n smaller tuples."""

return zip(*[iter(s)] * n)

....or...

chop = lambda s, n: zip(*[iter(s)] * n)

I was wondering if something like this already exists in Python proper
somewhere, perhaps in an optimized way? (I checked itertools, but I
didn't see anything right away).

Furthermore, what do people think about the idea of adding a truly
empty, no-op global lambda somewhere in Python? I use them a lot
(usually defining a: empty = lambda *a, **k: None somewhere at the
topmost module space), but if enough people did too, it might be worth
adding an empty() builtin to much later versions of Python.

Anyways, if these are stupid ideas I'm not surprised. Just some things
I've been thinking about and figured I'd toss out there on my lunch
break. :)

May 26 '06 #1
6 3610
On 27/05/2006 2:54 AM, Jeremy L. Moles wrote:

["chop" snipped]

Furthermore, what do people think about the idea of adding a truly
empty, no-op global lambda somewhere in Python? I use them a lot
What is the use case? Why write something like """empty(foo, 42,
cmd="xyzzy")""" when you could merely write "pass" or nothing at all?
(usually defining a: empty = lambda *a, **k: None somewhere at the
topmost module space), but if enough people did too, it might be worth
adding an empty() builtin to much later versions of Python.


May 26 '06 #2
John Machin <sj******@lexicon.net> writes:
What is the use case? Why write something like """empty(foo, 42,
cmd="xyzzy")""" when you could merely write "pass" or nothing at all?


The function might be a parameter to something.
May 26 '06 #3
On Sat, 2006-05-27 at 06:22 +1000, John Machin wrote:
On 27/05/2006 2:54 AM, Jeremy L. Moles wrote:

["chop" snipped]

Furthermore, what do people think about the idea of adding a truly
empty, no-op global lambda somewhere in Python? I use them a lot


What is the use case? Why write something like """empty(foo, 42,
cmd="xyzzy")""" when you could merely write "pass" or nothing at all?


Well, in a lot of the libraries I use, there is often a need for a
callback of some sort. Sure, I--and most other people--often define an
empty, no-op function of some sort when you want to at least have
"something" there, but I thought it might be kinda' neat to be able to
have a builtin called empty() or noop() that returned a (possibly
optimized?) function that just did nothing. :) It probably is a dumb
idea, hehe... the chop() was the big one I thought people might comment
on.

For example, I often do stuff like this for handling "input events" in
soya:

evhandlers = [emptyfunc for i in range(MAX_NUM_EVENTS)]

evhandlers[EVENT_NUMBER] = self.my_handler_function

....and then in the actual input loop I'll come along and say...

evhandlers[ev](*args)

In this way I don't have to test the value of any event and behave
accordingly--I just send it on it's way.

It's just an iterative way to say, "Okay, give me some default behavior
for everything, and I'll come back around later and set the explicit
handlers later."

This same design is probably used in other areas too... that's why I
brought up the "truly empty noop" thing; it would give me fuzzy feeling
to know that in those cases where empty() is defined, nothing is
happening (although, this could currently be the case in Python, I'm not
sure--I don't know a lot about Python internals).

Again, it's not a big deal. Just wondered what people thought. :)
(usually defining a: empty = lambda *a, **k: None somewhere at the
topmost module space), but if enough people did too, it might be worth
adding an empty() builtin to much later versions of Python.


May 26 '06 #4
On 27/05/2006 6:41 AM, Paul Rubin wrote:
John Machin <sj******@lexicon.net> writes:
What is the use case? Why write something like """empty(foo, 42,
cmd="xyzzy")""" when you could merely write "pass" or nothing at all?


The function might be a parameter to something.


Please bear with me; it's only about 7 a.m. in this neck of the woods
and I've had only 2 cups of coffee so far :-)

So one is calling some functions defined like:

def something1(..., afunc, ...):
...
if afunc([], 1.23, "plugh") is None:
....

and

def something2(bfunc, ...):
...
if bfunc(sys.stdout, "teehee", 0666) is None:
....
etc etc

and there are so many xfuncs with different argument signatures and so
many times that the caller wants to supply a do-nothing xfunc that
defining a swallow-any-and-all-args empty() function is warranted???

Thanks in advance for any enlightenment,
Cheers,
John
May 26 '06 #5
Jeremy L. Moles>It's just an iterative way to say, "Okay, give me some
default behavior for everything, and I'll come back around later and
set the explicit handlers later."<

There's some correlation with the Null Object pattern:
http://www.cs.oberlin.edu/~jwalker/nullObjPattern/

Bye,
bearophile

May 27 '06 #6
On 27/05/2006 7:10 AM, Jeremy L. Moles wrote:
On Sat, 2006-05-27 at 06:22 +1000, John Machin wrote:
On 27/05/2006 2:54 AM, Jeremy L. Moles wrote:

["chop" snipped]
Furthermore, what do people think about the idea of adding a truly
empty, no-op global lambda somewhere in Python? I use them a lot What is the use case? Why write something like """empty(foo, 42,
cmd="xyzzy")""" when you could merely write "pass" or nothing at all?


Well, in a lot of the libraries I use, there is often a need for a
callback of some sort. Sure, I--and most other people--often define an
empty, no-op function of some sort when you want to at least have
"something" there, but I thought it might be kinda' neat to be able to
have a builtin called empty() or noop() that returned a (possibly
optimized?) function that just did nothing. :) It probably is a dumb
idea, hehe... the chop() was the big one I thought people might comment
on.

For example, I often do stuff like this for handling "input events" in
soya:

evhandlers = [emptyfunc for i in range(MAX_NUM_EVENTS)]

evhandlers[EVENT_NUMBER] = self.my_handler_function

...and then in the actual input loop I'll come along and say...

evhandlers[ev](*args)

In this way I don't have to test the value of any event and behave
accordingly--I just send it on it's way.

It's just an iterative way to say, "Okay, give me some default behavior
for everything, and I'll come back around later and set the explicit
handlers later."


If you intend to set explicit handlers later, wouldn't it be better for
the default event handler to raise an exception, just in case your
intention is waylaid or sidetracked?

This same design is probably used in other areas too... that's why I
brought up the "truly empty noop" thing; it would give me fuzzy feeling
to know that in those cases where empty() is defined, nothing is
happening (although, this could currently be the case in Python, I'm not
sure--I don't know a lot about Python internals).


It would give you a fuzzy feeling to know that nothing is happening --
instead of *what* happening? What do you dread?

If, when you do evhandlers[ev](*args), evhandlers[ev] can't be evaluated
to a callable object (for one of several possible reasons), Python will
raise the appropriate exception. It won't "do nothing". Python will not
assume that you meant to supply a no-op function. Python refuses to
guess what you had in mind. This is part of the language philosophy, and
is not implementat(ion|er)-dependant.

I suggest that you fire up a Python interactive prompt, and type

import this

Cheers,
John
May 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5012
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
8862
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
7
11858
by: Dan | last post by:
I was trying to troubleshoot a login page that doesn't work - it keeps saying the login/password is missing - when my tracing discovered this peculiar behavior. register_globals is off, so at...
1
1922
by: gusmeister | last post by:
'chop @array' removes the last character for each element in an array. If I want to do the inverse and append a character to each element, how would I do that (without using a 'foreach' loop)?
3
1972
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
3
1405
by: scorpion53061 | last post by:
I have little hope of resolving this as I have had to contact outside help. But I thought I would post it here to see if anyone could add an idea or solution. 1. I have a form in a Class...
8
5712
by: meendar | last post by:
what will a object of an Empty class( contain nothing), do on default.What are all the default methods it calls. what is the use of creating the object for an empty class?
2
15576
by: perl_begi | last post by:
I am having a file that contains different strings, which in turn are name of folders. For example test. file has: test0002 intel/drivers/ Here there can be spaces, tabs after folder names...
59
8730
by: Steve R. Hastings | last post by:
So, Python 2.5 will have new any() and all() functions. http://www.python.org/dev/peps/pep-0356/ any(seq) returns True if any value in seq evaluates true, False otherwise. all(seq) returns...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.