Something is rotten in Denmark...

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jun 2 13:22:47 EDT 2011


On Thu, 02 Jun 2011 10:55:49 -0500, harrismh777 wrote:

> Steven D'Aprano wrote:
>> What do you expect this code to do?
>>
>> a = 42
>> funcs = [(lambda x: x+a) for i in range(10)] funcs[0](1)
> 
>     I do see your point with this... truly... but it did get me to think
> about what I *do* expect... and that is that 'a' (for the lambda) will
> be whatever 'a' is (now) at the time when the anonymous function is
> returned, not later when it is called (not really).

Arguably, there's nothing wrong with early binding as a strategy. It's 
just a different strategy from late binding.

It seems to me that early binding is less flexible than late, because 
with late binding you have a chance to simulate early binding by saving a 
reference of the variable elsewhere, such as in a default value, or an 
instance attribute. But with early binding, you're stuck. There's no 
simple or straightforward way to simulate late binding in an early 
binding language.



-- 
Steven



More information about the Python-list mailing list