Unification of Methods and Functions

Antoon Pardon apardon at forel.vub.ac.be
Tue May 11 07:12:35 EDT 2004


Op 2004-05-11, Delaney, Timothy C (Timothy) schreef <tdelaney at avaya.com>:
> 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.

I don't see how this contradicts what I want to say. In both case you have
something of the form

  obj = cls()

And in both obj.method() is equivallent to cls.method(obj)

> 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.

That it is an implementation detail doesn't contradict that it
is implicit.

-- 
Antoon Pardon



More information about the Python-list mailing list