Unification of Methods and Functions
Delaney, Timothy C (Timothy)
tdelaney at avaya.com
Tue May 11 04:10:36 EDT 2004
Antoon Pardon wrote:
> I'm a bit sick of this argument. There is a lot om implicity
> going on in python. if obj belongs to cls then obj.method()
> is syntactic sugar for cls.method(obj). That looks like
> a big implicite way to handle things in python.
Actually, they're not strictly the same. The instance knows which class
it is an instance of - that's why the syntactic sugar can work.
What you've stated is saying:
class C:
def f (self):
pass
c = C()
then c.f() is identical to calling C.f(c)
Now, in the above case that's correct. However, when subclasses get
introduced that goes out the door.
class D (C):
def f (self):
pass
d = D()
Now, d.f() is *not* identical to C.f(d). Instead it should be D.f(d).
Without knowing the class that d is an instance of, it is not possible
to call the correct method without the syntactic sugar.
d.f() is closer to syntactic sugar for d.__class__.f(d). However, even
that's not strictly correct, because c.f and d.f are bound methods -
they know which instance they are bound to without any further reference
to that instance.
Yes - there is a lot hidden here - but what is hidden is purely
implementation details.
Tim Delaney
More information about the Python-list
mailing list