Attributes of builtin/extension objects

Steven Bethard steven.bethard at gmail.com
Wed Nov 2 10:51:05 EST 2005


George Sakkis wrote:
> - Where do the attributes of a datetime.date instance live if it has
> neither a __dict__ nor __slots__ ?
> - How does dir() determine them ?

py> from datetime import date
py> d = date(2003,1,23)
py> dir(date) == dir(d)
True
py> for attr_name in ['day', 'month', 'year']:
...     attr_val = getattr(date, attr_name)
...     print attr_name, type(attr_val)
...
day <type 'getset_descriptor'>
month <type 'getset_descriptor'>
year <type 'getset_descriptor'>

So all the instance "attributes" are actually handled by descriptors on 
the type.  So datetime.date objects don't really have any instance 
attributes...

 > - dir() returns the attributes of the instance itself, its class and
 > its ancestor classes. Is there a way to determine the attributes of
 > the instance alone ?

I'm just guessing now, but perhaps if no __dict__ or __slots__ is 
available, all instance "attributes" are managed by descriptors on the type?

STeVe



More information about the Python-list mailing list