class static variables and __dict__

Zack goldsz at gmail.com
Sun Feb 17 00:16:43 CET 2008


Diez B. Roggisch wrote:
> Zack schrieb:
>> If I have a class static variable it doesn't show up in the __dict__ 
>> of an instance of that class.
>>
>> class C:
>>    n = 4
>>
>> x = C()
>> print C.__dict__
>> {'__module__': '__main__', '__doc__': None, 'n': 4}
>> print x.__dict__
>> {}
>>
>> This behavior makes sense to me as n is not encapsulated in x's 
>> namespace but what method can you use on x to find all available 
>> attributes for that class?
> 
> x.__class__.__dict__
> 
> Diez
> 

This would leave out any attributes of base classes. Not that I asked 
for that functionality in my original post but is there a way to get all 
  attributes qualified by x. ? I see that I could walk the dict of x, 
x.__class__ and x.__class__.__bases__ until I exhaust the tree. But is 
there a built in method for doing this?

-- 
Zack



More information about the Python-list mailing list