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.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to