473,503 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Metaclass discussons

Hi everyone!

I was just part of a small discussion concerning metaclass features on
Python, and one limitation that was appointed was the inability to
redefine metaclasses on existent code. Than I thought it was a good
way to explore some more the metaclass concepts..

Basically, people were arguing that there are some uses for that, based
on plugging new knowledge on existent classes and instances. Without
complaining too much about usage issues, I started looking for ways to
dismiss that limitation.

Without yet considering interpreter hacks, that's what I had as a first
solution (a very limited one, I must admit):

class M(type): pass
__metaclass__ = M
class object:
__metaclass__ = M
execfile("somemodule.py", globals(), locals())

Can you imagine any alternative which is not based on the same concept
(executing the module code on a hacked context)?

--
Gustavo Niemeyer
http://niemeyer.net

Jul 18 '05 #1
5 1502
Gustavo Niemeyer <ni******@conectiva.com> wrote previously:
|Can you imagine any alternative which is not based on the same concept
|(executing the module code on a hacked context)?

In Gnosis Utilities, the function gnosis.magic.import_with_metaclass()
does basically the same thing, but a bit more neatly packaged up. You
might give that a try for imposing metaclasses on older code.

Michele Simionato has proposed some enhancements to resolve issues of
metatype conflicts, but I haven't quite decided to bundle them up in
Gnosis, since that add quite a bit of complication that is not needed
95% of the time. But one day, I suppose I will. However, he has posted
the same general concepts to ActiveState's Python Cookbook; and the
article by my Michele and I that discusses some of this should FINALLY
appear at IBM developerWorks this week (we wrote it quite some months
ago; and it's been available on my website for some of the interim).

Yours, David...

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons. Intellectual
property is to the 21st century what the slave trade was to the 16th.
--
X-Shameless-Plug: Buy Text Processing in Python: http://tinyurl.com/jskh
Jul 18 '05 #2
Gustavo Niemeyer <ni******@conectiva.com> wrote in message news:<ma*********************************@python.o rg>...
Hi everyone!

I was just part of a small discussion concerning metaclass features on
Python, and one limitation that was appointed was the inability to
redefine metaclasses on existent code. Than I thought it was a good
way to explore some more the metaclass concepts..

Basically, people were arguing that there are some uses for that, based
on plugging new knowledge on existent classes and instances. Without
complaining too much about usage issues, I started looking for ways to
dismiss that limitation.

Without yet considering interpreter hacks, that's what I had as a first
solution (a very limited one, I must admit):

class M(type): pass
__metaclass__ = M
class object:
__metaclass__ = M
execfile("somemodule.py", globals(), locals())

Can you imagine any alternative which is not based on the same concept
(executing the module code on a hacked context)?


Oh, what a wonderfully evil hack! Thanks Gustavo, I never come to my
mind that I could redefine the object class!
I was using the hack of modifing the source code by adding a __metaclass__
atribute and re-exec-uting it, but I didn't like the trick. Then I wrote
a script to regenerate an entire hierarchy with a different metaclass.
Much less hackish, but a bit non-trivial. Your idea is perfect for easy
hacks in the debugging phase (i.e. add a metaclass generating logging
capabilities or other more general debugging utilities). You can
spell it even as

class object(object):
class __metaclass__(type):
pass

# rest of the code

Michele Simionato, Ph. D.
Mi**************@libero.it
http://www.phyast.pitt.edu/~micheles
--- Currently looking for a job ---
Jul 18 '05 #3
On 24 Aug 2003 04:46:26 -0700
mi**@pitt.edu (Michele Simionato) wrote:
Much less hackish, but a bit non-trivial. Your idea is perfect for
easy hacks in the debugging phase (i.e. add a metaclass generating
logging capabilities or other more general debugging utilities). You
can spell it even as

class object(object):
class __metaclass__(type):
pass

# rest of the code


Humm... but it does't propagate to imported modules

# mod.py

class Klass(object): pass

# end

---
class object(object): .... class __metaclass__(type): pass
.... import mod
mod.Klass.__bases__ (<type 'object'>,) class A(object): pass .... A.__bases__ (<class '__main__.object'>,)
--

But assigning it to __builtin__.object do:
class metaclass(type): pass .... import __builtin__
__builtin__.object = metaclass("object", (), {})
import mod
mod.Klass.__bases__ (<class '__main__.object'>,) class A(object): pass .... A.__bases__ (<class '__main__.object'>,)

Rgds

Pedro


Jul 18 '05 #4
On 24 Aug 2003 04:46:26 -0700
mi**@pitt.edu (Michele Simionato) wrote:
Much less hackish, but a bit non-trivial. Your idea is perfect for
easy hacks in the debugging phase (i.e. add a metaclass generating
logging capabilities or other more general debugging utilities). You
can spell it even as

class object(object):
class __metaclass__(type):
pass

# rest of the code


Humm... but it does't propagate to imported modules

# mod.py

class Klass(object): pass

# end

---
class object(object): .... class __metaclass__(type): pass
.... import mod
mod.Klass.__bases__ (<type 'object'>,) class A(object): pass .... A.__bases__ (<class '__main__.object'>,)
--

But assigning it to __builtin__.object do:
class metaclass(type): pass .... import __builtin__
__builtin__.object = metaclass("object", (), {})
import mod
mod.Klass.__bases__ (<class '__main__.object'>,) class A(object): pass .... A.__bases__ (<class '__main__.object'>,)

Rgds

Pedro


Jul 18 '05 #5
Hello Michele!
class object(object):
class __metaclass__(type):
pass


Thanks for the hint! After sending I found out that it should have
the real object as parent. OTOH, your nested definition is really
elegant! :-)

--
Gustavo Niemeyer
http://niemeyer.net

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1591
by: Robin Becker | last post by:
A colleague wanted to initialize his class __new__ and tried code resembling this #######################1 class Metaclass (type): def __init__(cls, name, bases, *args, **kwargs):...
5
2248
by: Irmen de Jong | last post by:
Hi, I've developed the Metaclass below, because I needed a way to make a bunch of classes thread-safe. I didn't want to change every method of the class by adding lock.aqcuire()..lock.release()...
33
2490
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
2
1924
by: zipher | last post by:
After searching through comp.lang.python and the web regarding metaclasses, I could not find an example for customing classes using metaclass parameters. I want to be able to create a class at...
16
1597
by: ironfroggy | last post by:
Hoping this isn't seeming too confusing, but I need to create a metaclass and a class using that metaclass, such that one of the bases of the metaclass is the class created with that metaclass. I...
14
2011
by: Pedro Werneck | last post by:
Hi I have a class A, with metaclass M_A, and class B, subclass of A, with metaclass M_B, subclass of M_A. A class C, subclass of B must have M_B or a subclass of it as metaclass, but what if...
9
1642
by: Christian Eder | last post by:
Hi, I think I have discovered a problem in context of metaclasses and multiple inheritance in python 2.4, which I could finally reduce to a simple example: Look at following code: class...
4
3287
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
2
1951
by: lbolognini | last post by:
Hi all, I dare risk my brain exploding by reaching for the understanding of metaclasses. At first i thought i almost got them, even if vaguely back in a corner of my mind, my understanding...
0
7203
marktang
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,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
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,...
0
7334
jinu1996
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...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
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...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.