Early and late binding [was Re: what does 'a=b=c=[]' do]

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Dec 24 03:26:59 EST 2011


On Fri, 23 Dec 2011 19:24:44 -0500, Devin Jeanpierre wrote:

>> To fake early binding when the language provides late binding, you
>> still use a sentinel value, but the initialization code creating the
>> default value is outside the body of the function, usually in a global
>> variable:
>>
>>    _DEFAULT_Y = []  # Private constant, don't touch.
>>
>>    def func(x, y=None):
>>        if y is None:
>>            y = _DEFAULT_Y
>>        ...
>>
>> This separates parts of the code that should be together, and relies on
>> a global, with all the disadvantages that implies.
> 
> No, you can just do def func(x, y=_DEFAULT_Y): ...

Point taken. Nevertheless, the semantics are still not the same as actual 
early binding: if the global name is deleted or changed, the function 
stops working.



-- 
Steven



More information about the Python-list mailing list