Warning when new attributes are added to classes at run time

Bruno Desthuilliers onurb at xiludom.gro
Thu Jul 20 10:01:03 EDT 2006


Matthew Wilson wrote:
> On Thu 20 Jul 2006 04:32:28 AM EDT, Bruno Desthuilliers wrote:
(snip)
> 
>>>    class C1(C):
>>>
>>>        standard_attributes = ['a1', 'a2']
>>
>>DRY violation here. And a potential problem with inheritance (as always
>>with class attributes).
> 
> 
> Considering I had to look up what DRY meant before replying to this
> message, I may be missing your point.  Is the repeat here that each
> subclass has to define its own list of standard attributes?   Or, is it
> that the standard_attributes list holds strings, but I could build that
> list up by looking at my existing attributes?

Mostly the second. But now, how are you going to build that list from
"existing attributes" before these attributes exists ?-) (chicken and
egg problem here...)

FWIW, you could use custom descriptors to build the 'schema', then use a
metaclass to detect these descriptors and build the "allowed attributes"
list. But the whole thing seems overkill to me if it's only to solve
typo problems (can be interesting for other reasons...).

Re-read the three first answers to your post - unit-tests,
pylint/pychecker, eventually slots...

My 2 cents

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list