By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Attributes of builtin/extension objects

P: n/a
Hi all,

I have a few questions on object introspection of builtins and
extension modules. Here's an example:
from datetime import date
d=date(2003,1,23)
d.__dict__ Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'datetime.date' object has no attribute '__dict__' d.__slots__ Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'datetime.date' object has no attribute '__slots__' dir(d)

['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__le__',
'__lt__', '__ne__', '__new__', '__radd__', '__reduce__',
'__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__str__',
'__sub__', 'ctime', 'day', 'fromordinal', 'fromtimestamp',
'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month',
'replace', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal',
'weekday', 'year']

- Where do the attributes of a datetime.date instance live if it has
neither a __dict__ nor __slots__ ?
- How does dir() determine them ?
- 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 ?

TIA,
George

Nov 2 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Nov 2 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.