[Python-Dev] pydoc for named tuples is missing methods
Michael Foord
fuzzyman at voidspace.org.uk
Wed Mar 16 17:54:33 CET 2011
On 15/03/2011 19:57, Greg Ewing wrote:
> Nick Coghlan wrote:
>
>> The challenge here is how it would interact with inheritance. pydoc
>> couldn't use normal attribute lookup, it would have to walk the MRO
>> manually,
>
> This is an instance of a pattern that I've encountered a
> few times in things that I've done: you have a class
> attribute containing a list of things, and you want it
> to be "additive" with respect to inheritance -- i.e. it
> contains the items specified in a particular class plus
> all those specified in its base classes.
>
> This can obviously be arranged using appropriate metaclass
> hackery, but I'm wondering whether it could be provided
> using some mechanism that can be applied orthogonally
> to any class attribute.
>
Right, I recently came across a similar usecase where a class needed to
extend an object defined in a base class. What we *wanted* to do was the
following:
class Foo(object):
thing = Thing()
class Bar(Foo):
thing.baz = 3
This doesn't work for obvious reasons. One potential solution was
re-assigning 'thing' on Bar as well and using a metaclass to merge them.
I think we ended up doing this, which is *slightly* ugly but not too bad:
class Bar(Foo):
thing = Foo.thing
thing.baz = 3
All the best,
Michael
> Maybe this is another reason to have a hook somewhere
> in the standard class creation process that allows a
> descriptor to initialise itself with knowledge of its
> environment.
>
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
More information about the Python-Dev
mailing list