[Python-ideas] Allow lambda decorators

spir denis.spir at free.fr
Mon Feb 9 11:09:30 CET 2009

Le Mon, 09 Feb 2009 01:03:39 -0500,
Terry Reedy <tjreedy at udel.edu> a écrit :

> To me, one pretty obvious way to define default non-parameters would be
> to follow the signature with "; <name = expr>+".  Where is the PEP, though?

I guess mean the following?

>>> def func_maker():
...     fs = []
...     for i in range(10):
...         def f(n=i):
...             return n
...         fs.append(f)
...     return fs
>>> fs = func_maker()
>>> for f in fs: print f(),
0 1 2 3 4 5 6 7 8 9

As I understand it, the issue only happens when yielding functions. For instance, the following works as expected:

class C(object):
	def __init__(self,n):
		self.n = n
def obj_maker():
	objs = []
	for i in range(10):
		obj = C(i)
	return objs

The pseudo-parameter trick used to generate funcs is only a workaround, but it works fine and is not overly complicated (I guess). Maybe this case should be documented in introductory literature; not only as a possible trap, also because it helps understanding python's (non-)scoping rules which allow (this is not be obvious when expecting iteration-specific scope):

# name 'item' like silently introduced here
for item in container:
	if test(item):
print item

la vida e estranya

More information about the Python-ideas mailing list