Metaclass discussons

G

Gustavo Niemeyer

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)?
 
D

David Mertz

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

Michele Simionato

Gustavo Niemeyer said:
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.
(e-mail address removed)
http://www.phyast.pitt.edu/~micheles
--- Currently looking for a job ---
 
P

Pedro Werneck

On 24 Aug 2003 04:46:26 -0700
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 __metaclass__(type): pass
.... (<class '__main__.object'>,)


--

But assigning it to __builtin__.object do:


Rgds

Pedro
 
P

Pedro Werneck

On 24 Aug 2003 04:46:26 -0700
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 __metaclass__(type): pass
.... (<class '__main__.object'>,)


--

But assigning it to __builtin__.object do:


Rgds

Pedro
 
G

Gustavo Niemeyer

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! :)
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top