[Python-3000] PEP: Information Attributes (original) (raw)

Raymond Hettinger python at rcn.com
Tue May 1 08:25:42 CEST 2007


Proto-PEP: Information Attributes (First Draft)

Proposal:

Testing hasattr() is a broadly applicable and flexible technique that works well whenever the presence of a method has an unambiguous interpretation (i.e. hash for hashability, iter for iterability, len for sized containers); however, there are other methods with ambiguous interpretations that could be resolved by adding an information attribute.

Motivation:

Signal attributes are proposed as a lightweight alternative to abstract base classes. The essential argument is that duck-typing works fairly well and needs only minimal augmentation to address a small set of recurring challenges. In contrast, the ABC approach imposes an extensive javaesque framework that cements APIs and limits flexibility. Real world Python programming experience has shown little day-to-day need for broad-sweeping API definitions; instead, there seem to be only a handful of recurring issues that can easily be addressed by a lightweight list of information attributes.

Use Cases with Ambiguous Interpretations

Advantages

The attribute approach is dynamic (doesn't require inheritance to work). It doesn't require mucking with isinstance() or other existing mechanisms. It restricts itself to making a limited, useful set of assertions rather than broadly covering a whole API. It leaves the proven pythonic notion of duck-typing as the rule rather than the exception. It resists the temptation to freeze all of the key APIs in concrete.



More information about the Python-3000 mailing list