Metaclass with name overloading.

Thomas Heller theller at python.net
Mon Sep 27 11:24:17 EDT 2004


Jacek Generowicz <jacek.generowicz at cern.ch> writes:

> I would like to write a metaclass which would allow me to overload
> names in the definition of its instances, like this
>
> class Foo(object):
>
>     __metaclass__ = OverloadingClass
>
>     att = 1
>     att = 3
>
>     def meth(self):
>         pass
>
>     def meth(self, arg):
>         return arg
>
>
> I would then like the dictionary received by OverloadingClass.__new__
> to look something like this:
>
> {'att': (1,3),
>  'meth: (<function meth at 0x4018e56c>, <function meth at 0x4018e80c>) }
>
> IOW, each name bound in the class definition should have associated
> with it, a tuple containing all the objects which were bound to that
> name, rather merely keeping the most recent binding for any given
> name.
>
> I was wondering whether it would be possible to achieve this by
> forcing Python to use some dicitonary proxy (which accumulates values,
> rather that keeping just the last value to be associated with a key),
> instead of dict, when executing the class definiton?
>
> Is something like this at all possible in pure Python? or does in
> require fiddling around in the guts of the parser?

It won't work for ordinary attributes, but for overloading methods you
should be able to play some tricks with decorators and sys._getframe().

Thomas



More information about the Python-list mailing list