Attribute reference design

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Jul 2 10:13:46 CEST 2008

chamalulu a écrit :
> On Jul 2, 1:17 am, Gary Herron <gher... at> wrote:
>> No need.   Also, you can define a class attribute (C++ might call it a
>> static attribute) and access it transparently through an instance.
>> class C:
>>   aClassAttribute = 123
>>   def __init__(self, ...):
>>     ...
>> c = C()
>> ... do something with c.aClassAttribute ...
> Actually, this is why I started too look into the attribute reference
> mechanics to begin with. Coming from mostly C# development I think
> it's weird to be able to refer to class attributes (static members)
> through a class instance (object). But I think I'm getting the
> picture. Function objects lay flat in memory (some heap...).

Python's functions are ordinary objects, instance of type 'function'.

> When
> defined inside classes they are wrapped in method objects.

Nope. The wrapping happens at lookup time, thru the descriptor protocol 
(the same thing that gives support for properties).

> When
> refered through classes or class instances they are unbound method
> objects or bound method objects respectively. 

That's what function.__get__() returns, yes. What is stored in the class 
object's __dict__ is the plain function.

> Am I on the right track?
> I still don't get why these methods show up when I dir() a class
> instance.

Help on built-in function dir in module __builtin__:

     dir([object]) -> list of strings

     Return an alphabetized list of names comprising (some of) the 
     of the given object, and of attributes reachable from it:

     No argument:  the names in the current scope.
     Module object:  the module attributes.
     Type or class object:  its attributes, and recursively the 
attributes of
         its bases.
     Otherwise:  its attributes, its class's attributes, and recursively the
         attributes of its class's base classes.


> /Henrik

More information about the Python-list mailing list