Metaclass with name overloading.

Carl Banks imbosol at aerojockey.com
Tue Sep 28 15:26:30 EDT 2004


Jacek Generowicz <jacek.generowicz at cern.ch> wrote in message news:<tyfekkoeyk2.fsf at pcepsft001.cern.ch>...
> 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
[snip]
> Is something like this at all possible in pure Python? or does in
> require fiddling around in the guts of the parser?


Not exactly what you asked for, and a bit (litotes) ugly, but it does
allow convenient subgroups within a class.  I used a similar trick
once when writing a little parser.


    def alltuple(name,bases,clsdict):
        return tuple(clsdict.values())


    class Foo(object):

        class att:
            __metaclass__ = alltuple
            _1 = 1
            _2 = 3

        class meth:
            __metaclass__ = alltuple
            def _1(self):
                pass
            def _2(self,arg):
                return arg


Making it nice and pretty left as an exercise.


-- 
CARL BANKS



More information about the Python-list mailing list