Another try at Python's selfishness

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Feb 3 06:51:15 EST 2006


On Fri, 03 Feb 2006 12:00:52 +0100, Magnus Lycka wrote:

> Today, Python has a syntactic shortcut. If 'a' is an
> instance of class 'A', a.f(x,y,z) is a shortcut for
> A.f(a,x,y,z). 

It is easy to work around (break?) that behaviour:

class A(object):
    def foo(self):
        print "normal method foo"

a = A()

def foo(self):
    print "special method foo"

from new import instancemethod
a.foo = instancemethod(foo, a)

Now we can see that a.foo() is no longer a shortcut for A.foo(a):

>>> a.foo()
special method foo
>>> A.foo(a)
normal method foo


So instances can have methods that they don't inherit from their class!



-- 
Steven.




More information about the Python-list mailing list