CONSTRUCT - Adding Functionality to the Overall System

I

Ilias Lazaridis

I understand that I can use __metaclass__ to create a class which
modifies the behaviour of another class.

How can I add this metaclass to *all* classes in the system?

(In ruby I would alter the "Class" class)

..

http://lazaridis.com
 
C

Calvin Spealman

I understand that I can use __metaclass__ to create a class which
modifies the behaviour of another class.

How can I add this metaclass to *all* classes in the system?

(In ruby I would alter the "Class" class)

This is a terrible idea. When my, Joe Modulewriter, released my
library of AwesomeCoolClasses, i except my metaclass to be something
particular and if it starts getting wonky and I do anything hackery on
my end with the types, then the end-user's computer might 'splode!

What I am trying to say, is its a terrible idea to go changing other
peoples' classes at runtime without knowing the real affects of what
is going on. If you are just going to shoot yourself in the foot, why
should we even give you a gun?
 
G

Georg Brandl

Ilias said:
I understand that I can use __metaclass__ to create a class which
modifies the behaviour of another class.

How can I add this metaclass to *all* classes in the system?

(In ruby I would alter the "Class" class)

You'd have to set

__metaclass__ = whatever

at the top of each module whose classes are to get the new behavior.

You can't alter classes which you don't control or create in your code.

Georg
 
D

Damjan

You'd have to set

__metaclass__ = whatever

at the top of each module whose classes are to get the new behavior.

I think '__metaclass__ = whatever' affects only the creation of classes that
would otherwise be old-style classes?
You can't alter classes which you don't control or create in your code.

I remeber I've seen an implementation of import_with_metaclass somewhere on
IBM's developerworks. I didn't quite undersntad it though.
 
I

Ilias Lazaridis

Damjan said:
I think '__metaclass__ = whatever' affects only the creation of classes that
would otherwise be old-style classes?

It seems so:

http://mail.python.org/pipermail/python-list/2003-June/166572.html
I remeber I've seen an implementation of import_with_metaclass somewhere on
IBM's developerworks. I didn't quite undersntad it though.

http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html

I am not so much interested in old-style, as is start production with
python 2.4 (possibly even with python 2.5).

..
 
S

Steve Holden

Ilias said:
It seems so:

http://mail.python.org/pipermail/python-list/2003-June/166572.html




http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html

I am not so much interested in old-style, as is start production with
python 2.4 (possibly even with python 2.5).
The fact remains that you won't be able to affect the built-in classes
such as int and str - they are hard-coded in C (for CPython, at least),
and so their metaclass is also implied and cannot be changed.

regards
Steve
 
P

Paul McGuire

Calvin Spealman said:
This is a terrible idea. When my, Joe Modulewriter, released my
library of AwesomeCoolClasses, i except my metaclass to be something
particular and if it starts getting wonky and I do anything hackery on
my end with the types, then the end-user's computer might 'splode!

What I am trying to say, is its a terrible idea to go changing other
peoples' classes at runtime without knowing the real affects of what
is going on. If you are just going to shoot yourself in the foot, why
should we even give you a gun?

Reminds me of when I was learning Smalltalk, and modified printOn (ST's
equivalent of str()) of the root-of-all-classes object class. I don't
remember the exact error I made, but it was a mess undoing it!

-- Paul
 
I

Ilias Lazaridis

Steve said:
Ilias Lazaridis wrote: ....

The fact remains that you won't be able to affect the built-in classes
such as int and str - they are hard-coded in C (for CPython, at least),
and so their metaclass is also implied and cannot be changed.

....except in C.

I assume the "root class" should be available/changeable in "C", too.

Do I have to change the sources directly, or does python provide
"C-level-extension/modication" mechanisms, which can be applied to
core-level classes, too?

Is the Python Object Model documented anywhere in a diagram, something
similar to this?:

http://case.lazaridis.com/wiki/RubyObjectModel

..
 
C

Calvin Spealman

...except in C.

I assume the "root class" should be available/changeable in "C", too.

Do I have to change the sources directly, or does python provide
"C-level-extension/modication" mechanisms, which can be applied to
core-level classes, too?

Is the Python Object Model documented anywhere in a diagram, something
similar to this?:

http://case.lazaridis.com/wiki/RubyObjectModel

What could you possibly want to do this for so badly that you would
shun all alternatives and even resort to hacking up the runtime at the
C-level to redefine core types in non-standard and unpredictable
ways?! Seriously, please give a good reason for every doing this. I
can't imagine there is any way it would justify all this in the face
of just looking for an alternative. You aren't trying to use python,
at this point, you are trying to fork it.
 
I

Ilias Lazaridis

Calvin said:
What could you possibly want to do this for so badly that you would
shun all alternatives and even resort to hacking up the runtime at the
C-level to redefine core types in non-standard and unpredictable
ways?! Seriously, please give a good reason for every doing this. I
can't imagine there is any way it would justify all this in the face
of just looking for an alternative. You aren't trying to use python,
at this point, you are trying to fork it.

http://case.lazaridis.com/wiki/Please#OffTopicPosts

..
 
G

Georg Brandl

Damjan said:
I think '__metaclass__ = whatever' affects only the creation of classes that
would otherwise be old-style classes?

Wrong.

If you set __metaclass__ = type, every class in that module will be new-style.

If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
class in that module will be new-style and have MyClass as a metaclass.

The usual way to create new-style classes, inheriting from object or another
new-style class, works because if no __metaclass__ is defined, the first
base class's class is taken as the metaclass.

Georg
 
D

Diez B. Roggisch

Wrong.

If you set __metaclass__ = type, every class in that module will be
new-style.

If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
class in that module will be new-style and have MyClass as a metaclass.

The usual way to create new-style classes, inheriting from object or
another
new-style class, works because if no __metaclass__ is defined, the first
base class's class is taken as the metaclass.


I was under that impression, too. But this behaves different (py2.4):


---- test.py ----
class meta(type):
def __new__(*args):
print args
return type(*args[1:])

__metaclass__ = meta

class OldStyle:
pass

class NewStyle(object):
#__metaclass__ = meta

pass



---- test.py ----

deets$ python2.4 /tmp/test.py
(<class '__main__.meta'>, 'OldStyle', (), {'__module__': '__main__'})
deets$

I was astonished to see that. Any explanation?

Diez
 
D

Damjan

__metaclass__ = whatever
Wrong.

If you set __metaclass__ = type, every class in that module will be
new-style.
<type 'classobj'>

It seems that NOT every class in my module will be a new style class,
especially those that inherit from other old-style classes in other
modules.
 
G

Georg Brandl

Diez said:
Wrong.

If you set __metaclass__ = type, every class in that module will be
new-style.

If you set __metaclass__ = MyClass, and MyClass inherits from <type>, every
class in that module will be new-style and have MyClass as a metaclass.

The usual way to create new-style classes, inheriting from object or
another
new-style class, works because if no __metaclass__ is defined, the first
base class's class is taken as the metaclass.


I was under that impression, too. But this behaves different (py2.4):
[...]

deets$ python2.4 /tmp/test.py
(<class '__main__.meta'>, 'OldStyle', (), {'__module__': '__main__'})
deets$

I was astonished to see that. Any explanation?

Yes. I was wrong, the order of lookup is reversed. This first base class's
class is used before the global __metaclass__.

So what Damjan said is correct: global __metaclass__ is only usable for
classes that don't have bases.

Whether this is desirable is another question...

Georg
 
I

Ilias Lazaridis

Ilias said:
I understand that I can use __metaclass__ to create a class which
modifies the behaviour of another class.

How can I add this metaclass to *all* classes in the system?

(In ruby I would alter the "Class" class)

I got confused from the discussion about __metaclass__.

possibly a little more practical.

I like to add a method "writeDebug(self, msg)" to all (or the most
possible) classes in the system.

How do I do this?

* with new style classes
* with old style classes
 
D

Diez B. Roggisch

Ilias said:
I got confused from the discussion about __metaclass__.

possibly a little more practical.

I like to add a method "writeDebug(self, msg)" to all (or the most
possible) classes in the system.

How do I do this?

* with new style classes
* with old style classes

OMFG. For asking the question (paraphrased) "why do you want to do this",
Mr. Spealman received an

http://case.lazaridis.com/wiki/Please#OffTopicPosts

But now you do exactly that - give more details. Maybe this could teach you
the lesson that the variance of input you get here to your questions is
worth considering?

But I've got the wicked feeling that this won't happen...

Diez
 
M

MonkeeSage

Ilias said:
How do I do this?

It's easy:

def writeDebug(msg):
print "I do not debug things, I _evaluate_ with professionals on the
industries! See ticket 547!\n" \
"Oh yeah, and %s" % msg
....
class Foo:
writeDebug("how can I configure the interpreter for understand
Klingon participals and noun-phrases? Must I rectify the standard
dictionaries first?")

Regards,
Jordan
 
I

Ilias Lazaridis

Diez said:
OMFG. For asking the question (paraphrased) "why do you want to do this",
Mr. Spealman received an

http://case.lazaridis.com/wiki/Please#OffTopicPosts

But now you do exactly that - give more details. Maybe this could teach you
the lesson that the variance of input you get here to your questions is
worth considering?

But I've got the wicked feeling that this won't happen...

I've not given more details, but less.

Just simplified my request.

..
 
I

Ilias Lazaridis

MonkeeSage said:
It's easy:

def writeDebug(msg):
print "I do not debug things, I _evaluate_ with professionals on the
industries! See ticket 547!\n" \
"Oh yeah, and %s" % msg

where do I place this function...
...
class Foo:
writeDebug("how can I configure the interpreter for understand
Klingon participals and noun-phrases? Must I rectify the standard
dictionaries first?")

....thus it becomes available within class "Foo" and all other Classes?

Something like a central import?

..
 
M

MonkeeSage

Ilias said:
where do I place this function...

The place where you want it to be.
...thus it becomes available within class "Foo" and all other Classes?

Anything defined in the top-level (i.e., the sys.modules['__main__']
namespace) is accessible in every scope...but I assume you already know
that.

You could also use a super-duper super class from which to derive all
your other classes, and add/replace any methods you want there:

class lazaridis(object):
def evaluate(self, community):
if 1 is 1:
print "Evaluating %s... Result: failed!" % community

class Foo(lazaridis, str):
pass

f=Foo('This has been a test of the lazaridis evaluation system')
f.evaluate('Python')
f.evaluate('Ruby')
f.evaluate('Java')
print f
Something like a central import?

That would probably be the most logical thing to do.

But again, I assume you already know all this, so why are you asking?
Is this part of the evaluation process?

Regards,
Jordan
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top