Dynamic attribute introspection

Guido van Rossum guido at python.org
Tue Aug 14 05:49:07 CEST 2001


"Neil Hodgson" <nhodgson at bigpond.net.au> writes:

> Python allows classes to define dynamic attributes with the
> __getattr__ and __setattr__ methods. This is useful in many
> circumstances, particularly when proxying to external objects such
> as those accessed through object brokers (COM, XPCOM, CORBA, ...),
> databases, and dynamically wrapped APIs.  However, there is no
> generic method for finding the attributes that are available through
> this feature even though the set of attributes is often known at
> runtime by the class.

This is what the new class mechanism attempts to address, in
particular PEP 252.  Please read that PEP.  I hope that it addresses
your concerns, and if it doesn't, I'd like to see what I can do to
address them.  As an intro to the PEP (and its companion, PEP 253),
please refer to the tutorial at http://python.org/2.2/descrintro.html.

With the new class system, many uses of __getattr__ can be replaced by
descriptors for get/set attributes; see the section "Attributes
defined by get/set methods" in the tutorial mentioned above.

Regarding your proposed _getAttributeNames() API, how would it
typically find the attribute names?

> It may also prove useful to define a mechanism to provide doc
> strings for dynamic attributes for display by development tools.

The descriptor mechanism propose in PEP 252 allows for this.

> On a procedural note, is defining a convention an appropriate
> subject for a PEP?

Definitely!

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list