inline metaclasses

G

gangesmaster

just something i thought looked nice and wanted to share with the rest
of you:
.... 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
 
M

Marc 'BlackJack' Rintsch

gangesmaster said:
just something i thought looked nice and wanted to share with the rest
of you:

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

Alex Martelli

Marc 'BlackJack' Rintsch said:
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
 
K

K.S.Sreeram

Marc said:
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

iD8DBQFEqd3Mrgn0plK5qqURAkz7AKCnzjBBoN4nll7IvUzMHizf4WagbACgw6tD
knPBr2MOqCJdL9wiCjTqNJY=
=9kKy
-----END PGP SIGNATURE-----
 
M

Marc 'BlackJack' Rintsch

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

K.S.Sreeram

Marc said:
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-----
 
M

Marc 'BlackJack' Rintsch

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top