By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

inline metaclasses

P: n/a
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

Jul 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In <11*********************@b68g2000cwa.googlegroups. com>, gangesmaster
wrote:
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?
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
Jul 3 '06 #2

P: n/a
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
In <11*********************@b68g2000cwa.googlegroups. com>, gangesmaster
wrote:
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?

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!?
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
Jul 4 '06 #3

P: n/a
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEqd3Mrgn0plK5qqURAkz7AKCnzjBBoN4nll7IvUzMHi zf4WagbACgw6tD
knPBr2MOqCJdL9wiCjTqNJY=
=9kKy
-----END PGP SIGNATURE-----

Jul 4 '06 #4

P: n/a
In <ma***************************************@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
Jul 4 '06 #5

P: n/a
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEqjM4rgn0plK5qqURApJZAJ47m7wlyphZqlw2VAqTP+ UcBlRfZACfR/i3
hIinuIKi7eNgZemfZAY5ejI=
=i/Gv
-----END PGP SIGNATURE-----

Jul 4 '06 #6

P: n/a
In <ma***************************************@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
Jul 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.