Applications Google
Menu principal

Post a Comment On: Only Python

"Correction: Javascript NOT more dynamic than Python"

9 Comments -

1 – 9 of 9
Blogger adam said...

You can certainly do that in Python.

from types import MethodType

class A(object):
def b(self):
print "B"

def c(self):
print "C"

a = A()
a.b = MethodType(c, a)
# prints "C"
a.b()

9:17 PM

Blogger André Roberge said...

That's different: it's adding a method to an instance. Using the example in my blogpost, with Javascript, if I had say 3 robots (r, s and t), they would all inherit the new method from changing their parent's prototype. Furthermore, I don't believe that you can remove a method from a child class in Python and have a lookup being made from that point to the parent's, as is the case with Javascript.

9:20 PM

Blogger André Roberge said...

Actually, your approach did not modify the parent class of A (which is object), unlike the example I gave. And, instead of using MethodType, I believe that you could simply have done a.b = c. This is just simple monkeypatching at the instance level - nothing on the lookup chain has been changed.

9:25 PM

Blogger adam said...

You can

del a.b

to get back access to the original method. You can also

a.__class__.b = c

or

type(a).b = c

to change the method on the class.

You can even replace

A.__bases__

with a new set of base classes if you want to change the type / MRO of A.

9:25 PM

Blogger adam said...

Also, you can do

class D(object):
def b(self):
print "D"

a.__class__ = D

and completely change the type / class of a.

9:28 PM

Blogger adam said...

In response to your second comment at 9:25:

You can't just do

a.b = c

if you want "self" to automatically get passed to "c" -- if you just assign the function, it's a static method, not an instance method.

9:30 PM

Blogger André Roberge said...

Adam, you are certainly correct about not being able to do a.b = c as I wrote - I stand corrected on that.

However, I still maintain that what you are suggesting is doing changes per instance. If one has multiple instances of the same type, I'm quite sure that the type of change you are suggesting would have to be done on every single instance - unlike the case with Javascript.

Perhaps there is a way to do it using metaclasses ... but I would be surprised.

9:40 PM

Blogger adam said...

I'm sorry if my examples weren't clear, but you can certainly alter a class object, either directly or through an instance, and therefore effect the behavior of all instances of that class.

>>> class A(object):
... def b(self):
... print "C"
...
>>> d = A()
>>> e = A()
>>> d.b()
C
>>> e.b()
C
>>> def f(self):
... print "G"
...
>>> e.__class__.b = f
>>> d.b()
G
>>> e.b()
G

10:07 PM

Blogger André Roberge said...

Thanks Adam for correcting me.

10:25 PM

Spammers: none shall pass.
You can use some HTML tags, such as <b>, <i>, <a>

Comments on this blog are restricted to team members.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot