473,594 Members | 2,779 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 3613
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(fo o, 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******@lexic on.net> writes:
What is the use case? Why write something like """empty(fo o, 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(fo o, 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_E VENTS)]

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******@lexic on.net> writes:
What is the use case? Why write something like """empty(fo o, 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(bfun c, ...):
...
if bfunc(sys.stdou t, "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(fo o, 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_E VENTS)]

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
5021
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 double-checked the path to my error log. It is in /var/www/logs/php_error_log Thanks. :) -Wayne Stevenson
3
8878
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 a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: <?php echo $_SERVER; ?>
7
11867
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 the top of my script I assign a few variables to incoming GET and POST values. $login = clean($_POST, 30); $passwd = clean($_POST, 30);
1
1925
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
1976
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 in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
3
1407
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 Control library along with 6 other namespaces. My root functions are in this form and all the other namespaces revolve around this form. I add the project as a dll to another windows project and call its functions there. Option Strict is
8
5714
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
15589
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 (like we can place space after test0002 or it may happen when you edit file in notepad and use same in Unix test0002 becomes test0002^M). I used chop and chomp to do it. What I did was I read all contents in array then I did chop (or chomp) for...
59
8758
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 True if all values in seq evaluate true, False otherwise. I have a question: what should these functions return when seq is an empty list?
0
7946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8003
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8234
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6654
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
1210
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.