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 6 1075
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
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
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-----
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
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-----
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephan Diehl |
last post by:
I have a question about metaclasses:
How would be the best way to "merge" different metaclasses?
Or, more precisely, what is the best way to merge metaclass functionality?
The idea is basicly...
|
by: Mike C. Fletcher |
last post by:
Slides from my PyGTA presentation on Tuesday, focusing mostly on
why/where you would want to use meta-classes, are available in PDF format:
...
|
by: Santiago Aguiar |
last post by:
Im trying to use AOP techniques on python but, even if I understand
the idea behind metaclasses, I cant find a way to use AOP in a dynamic
way.
What im trying to do is to weave an aspect to a...
|
by: Michele Simionato |
last post by:
> Hello, my name is Skip and I am metaclass-unaware. I've been programming in
> Python for about ten years and I have yet to write a metaclass. At first I
> thought it was just that metaclasses...
|
by: Michael Sparks |
last post by:
Anyway...
At Europython Guido discussed with everyone the outstanding issue with
decorators and there was a clear majority in favour of having them, which
was good. From where I was sitting it...
|
by: Jean-François Doyon |
last post by:
Hello,
I'm using MetaClasses to create classes.
How do I make these new classes "globally" available?
I probably just have to assign them to something magic, but I can't seem to
figure out...
|
by: Hartmut Goebel |
last post by:
Hi,
I'm currently implementing a tool for auto-generating webforms from a
desription file. The output should become a php-script, an asp-script,
zope-formulator or such. Each if it may (or may...
|
by: Jan-Ole Esleben |
last post by:
Hi!
I've just posted a question about metaclasses in ZOPE on the ZOPE
list, and one of the replies said that metaclasses (at least
"painless" metaclasses) cannot be used without new-style...
|
by: Alan Isaac |
last post by:
Forman's book is out of print.
Is there a good substitute?
Thanks,
Alan Isaac
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |