Style question - defining immutable class data members

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Mar 15 15:56:55 EDT 2009


Rhodri James a écrit :
> On Sun, 15 Mar 2009 17:55:25 -0000, Aaron Brady <castironpi at gmail.com> 
> wrote:
> 
>> On Mar 15, 12:39 pm, John Posner <jjpos... at snet.net> wrote:
(snip)

>>> Is there a beneficial effect of silently creating the instance 
>>> attribute, which outweighs the detrimental effects: (1) 
>>> inconsistency, (2) the "surprising" decoupling?
>>
>> Yes.  If you access an attribute, you want the search to go like this:
>>
>> - check instance for attribute
>> - check class for attribute
>> - check base classes for attribute
> 
> But do you, though?  The only occasion I can think of that I'd want
> the search to go past the instance is this "auto-initialisation",

How would you resolve inst.method, then ?

> and frankly I'd rather do that in an __init__ anyway.  Perhaps
> static methods or class methods work that way, I don't know how
> the innards of the interpreter handle that.
> 
> Is there any actual advantage to self.attribute picking up
> Class.attribute instead of raising a NameError?

Yes: resolving methods and other descriptors.




More information about the Python-list mailing list