Attributes of builtin/extension objects

G

George Sakkis

Hi all,

I have a few questions on object introspection of builtins and
extension modules. Here's an example:
Traceback (most recent call last):
Traceback (most recent call last):
['__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
 
S

Steven Bethard

George said:
- 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top