Ticket #5797 defect closed fixed
constants cannot be attributes of another class
| Reported by: | exarkun | Owned by: | exarkun |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | core | Keywords: | |
| Cc: | hs@… | Branch: | branches/constants-foreign-attribute-5797 |
| Author: | exarkun | Launchpad Bug: |
Description
This behavior is surprising and a bit unfortunate:
>>> from twisted.python.constants import Names, NamedConstant >>> >>> class Foo(Names): ... x = NamedConstant() ... >>> class Bar(object): ... a = Foo.x ... >>> Bar.a Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/exarkun/Projects/Twisted/trunk/twisted/python/constants.py", line 40, in __get__ cls._initializeEnumerants() AttributeError: type object 'Bar' has no attribute '_initializeEnumerants' >>> Bar().a Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/exarkun/Projects/Twisted/trunk/twisted/python/constants.py", line 40, in __get__ cls._initializeEnumerants() AttributeError: type object 'Bar' has no attribute '_initializeEnumerants' >>>
People generally expect to be able to take an object and make it an attribute of a class like this.
Perhaps NamedConstant needs to check the type of the class it's being looked up on? Alternatively, maybe the experiment in not using metaclasses is a failure and twisted.python.constants needs to be switched to using metaclasses.
Change History
Note: See
TracTickets for help on using
tickets.
