inline metaclasses
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Tue Jul 4 03:12:06 EDT 2006
In <mailman.7745.1151983064.27775.python-list at python.org>, K.S.Sreeram
wrote:
> Marc 'BlackJack' Rintsch wrote:
>> But why use a metaclass? If the meta class is only applied to *one*
>> class, can't you do at class level whatever the metaclass is doing!?
>
> The very fact that you can put a loop inside __metaclass__ may be reason
> enough for a one-off metaclass.
Ah, it's not the loop but the access to the `dict`! You can write loops
at class level too but I haven't found a way to access `X`s `__dict__`
because `X` does not exist at this point.
> Here's a contrived example:
>
> class X :
> def __metaclass__( name, bases, dict ) :
> for k,v in dict.items() :
> if k.startswith('get_') :
> dict[ k[4:].upper() ] = property( v )
> return type( name, bases, dict )
>
> def get_a( self ) :
> ...
>
> def get_b( self ) :
> ...
>
>
> o = X()
> print o.A
> print o.B
BTW, if that's what gangesmaster is after then it seem to work already.
Put ``(object)`` after ``X`` and return something, say 'a' and 'b', in the
getters and the example prints 'a' and 'b'.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list