Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

inline metaclasses

0 views
Skip to first unread message

gangesmaster

unread,
Jul 3, 2006, 2:42:57 PM7/3/06
to
just something i thought looked nice and wanted to share with the rest
of you:

>>> class x(object):
... def __metaclass__(name, bases, dict):
... print "hello"
... return type(name, bases, dict)
...
hello
>>>

instead of defining a separate metaclass function/class, you can do
it inline. isn't that cool?


-tomer

Marc 'BlackJack' Rintsch

unread,
Jul 3, 2006, 6:29:39 PM7/3/06
to
In <1151952177....@b68g2000cwa.googlegroups.com>, gangesmaster
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!?

Ciao,
Marc 'BlackJack' Rintsch

Alex Martelli

unread,
Jul 3, 2006, 8:50:00 PM7/3/06
to

Most but not all of the "whatever". E.g.:

class X:
class __metaclass__(type):
def __str__(cls): return 'The great class X!'

print X


You can't make "print X" behave arbitrarily w/o a custom metaclass.


Alex

K.S.Sreeram

unread,
Jul 3, 2006, 11:17:26 PM7/3/06
to pytho...@python.org
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.

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

signature.asc

Marc 'BlackJack' Rintsch

unread,
Jul 4, 2006, 3:12:06 AM7/4/06
to
In <mailman.7745.1151983...@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

K.S.Sreeram

unread,
Jul 4, 2006, 5:21:56 AM7/4/06
to pytho...@python.org
Marc 'BlackJack' Rintsch wrote:

> K.S.Sreeram wrote:
>> 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.

You're right. I guess i wasn't clear in my previous post, but I was
referring to 'the ability to process the dict (say, using loops)'

> 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'.

btw, the example seems to work even with old-style classes.

Regards
Sreeram

signature.asc

Marc 'BlackJack' Rintsch

unread,
Jul 4, 2006, 7:42:19 AM7/4/06
to
In <mailman.7757.1152004...@python.org>, K.S.Sreeram
wrote:

>> 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'.
>
> btw, the example seems to work even with old-style classes.

Yes, but setting properties works only with new-style classes. So I use
them whenever I use properties. In my mind properties and new-style
classes are linked together.

Ciao,
Marc 'BlackJack' Rintsch

0 new messages