On 22 Apr 2005 14:41:45 -0700, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> I was thinking about something like the following;
> 
> >>> a=[ t**n for n in range(4) ]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> NameError: name 't' is not defined
> >>>
> 
> or
> 
> >>> a=[ lambda t: t**n for n in range(4) ]
> >>> t=2
> >>> a
> [<function <lambda> at 0x403dcc6c>, <function <lambda> at 0x403dcca4>,
> <function <lambda> at 0x403dccdc>, <function <lambda> at 0x403dcd14>]
> >>> t=3
> >>> a
> [<function <lambda> at 0x403dcc6c>, <function <lambda> at 0x403dcca4>,
> <function <lambda> at 0x403dccdc>, <function <lambda> at 0x403dcd14>]
> >>>
> 

Well, everybody else took away your lambda (greedy people!) but I'm
here to say that it doesn't *have* to go away. I didn't think this
would be possible, but it is:

>>> t = 2
>>> [(lambda n: t**n)(n) for n in range(4)]
[1, 2, 4, 8]
>>> t = 3
>>> [(lambda n: t**n)(n) for n in range(4)]
[1, 3, 9, 27]

I just thought that was kinda neat. If you wanted to obfuscate some
python, this would be an awesome trick - hide the value of t somewhere
early in the function then pull a variation of this out later.

Peace
Bill Mill
bill.mill at gmail.com
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to