classmethod & staticmethod

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Wed Jul 25 21:12:01 EDT 2007


On Wed, 25 Jul 2007 00:55:17 +0000, Alex Popescu wrote:

> Neil Cerutti <horpner at yahoo.com> wrote in news:eRwpi.36813$G23.28496
> @newsreading01.news.tds.net:
> 
>> On 2007-07-25, Alex Popescu <nospam.themindstorm at gmail.com> wrote:
>>> As a matter of style, how do you figure out that class_list is
>>> a class attribute and not an instance attribute? (I don't
>>> remember seeing anything in the PEP describing the coding
>>> style).
>> 
>> Check out dir(MyClass) and dir(MyClass()) for some insight, if it
>> turns out that it matters. 
> 
> I must confess that I am a bit confused by this advise, as both are 
> returning exactly the same thing.
> 
>> Preferably, the user of a class
>> doesn't have to really think about it much.
>> 
> I know that this would be prefered, but in case you are getting 3rd party 
> code and you modify a class attribute without knowing it is a class 
> attribute then you may get into trouble (indeed the real problem is with 
> the designer of the 3rd party code, but still I think it is a valid 
> concern).

# warning: doesn't consider slots
if "attribute" in instance.__dict__:
    print "instance attribute"
elif "attribute" in instance.__class__.__dict__:
    print "class attribute"
else:
    print "either no attribute at all, or in a parent class"



-- 
Steven.




More information about the Python-list mailing list