anonymous functions? class?

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Nov 16 10:14:33 EST 2001


Erik Max Francis <max at alcyone.com> wrote in 
news:3BF4288A.FC68121F at alcyone.com:

> Yes.  Python has lambda functions for this, although there are some
> restrictions:   lambda functions have to take at least one argument
> (otherwise what's the point), and they can only evaluate to expressions,
> rather than whole statements.

Your second point is correct, but the first one is wrong. You can have a 
lambda with no arguments if you wish. The point is the same as any function 
with no arguments, if it calls other functions which use globals (or nested 
scope variables) or have side effects, then the lamda doesn't access the 
globals or produce the side effects until it is invoked.
e.g.
    	f = open('somefile')
    	readchop = lambda: f.readline()[:-1]
    	for i in range(10): print readchop()

is a not terribly good example.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list