Properties using metaclasses (was: Function/Method Decorator Syntax)

Michael Hudson mwh at python.net
Thu Jun 12 09:05:33 EDT 2003


Andrew Bennetts <andrew-pythonlist at puzzling.org> writes:

> A more extreme example of abusing class was shown to me at PyCon by a fellow
> Twisted developer (who shall remain nameless so that he won't get mobbed by
> angry hordes ;) ... it looked something like this:
> 
> ----
> class Adder(type):
>     def __new__(cls, name, bases, dict):
>         return reduce(lambda x,y: x+y, bases, 0)
> 
> class C(1, 2, 3, 4, 5):
>     __metaclass__ = Adder
> 
> print C
> ----
> 
> There's some things the class keyword was just never meant to do.

I managed do something like this:

class Add(GF, int, str):
    def call(i, s):
        print "int, str"

class Add(GF, int, int):
    def call(i, i):
        print "int, int"

then

>>> Add(1,2)
int, int
>>> Add(1,"a")
int, str

Can't find a version of the code that actually works now, though.

Cheers,
M.

-- 
  It *is*, however, rather juvenile, in contravention of the 
  Trades Descriptions Act, and will lead eventually to the Dragon 
  of Unhappiness flying up your bottom.      -- Peter Ellis, ucam.chat




More information about the Python-list mailing list