Promoting Python

Marko Rauhamaa marko at pacujo.net
Wed Apr 6 18:03:34 EDT 2016


Ian Kelly <ian.g.kelly at gmail.com>:
> On Wed, Apr 6, 2016 at 2:39 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Not convinced. Probably just an oversight.
>> It's documented here:
> https://docs.python.org/3/reference/datamodel.html#special-method-lookup

Ok, not an oversight but some inherent trouble with the way object
methods are related with their prototypes in the class. An unexpected
deficiency in the object-class relationship of sorts.
> The minidom implementation is probably just calling
> filelike.write('blah'). That will succeed in calling either a method
> or a function-in-an-attribute. Note that the write function you
> defined in this example has no self argument. This points to the fact
> that it's not truly a method.

No true Scotsman, there?
Once you look up an object method, it doesn't have a self argument. The
self argument is cooked up for the benefit of the function definition in
the class.
IOW, if I have this class:
 class A:
 def f(self):
 print("f")
and this object:
 a = A()
then,
 a.f
is a function that doesn't have a self argument. That function is
generated on the fly, and it delegates to A.f, providing it with self
argument.
However, a.f itself is just a function that doesn't accept any
arguments.
Defining:
 def f():
 print("f")
 a.f = f
simply short-circuits the attribute lookup process; no need to consult
the class when the attribute (method!) is right there in the object's
dict.
Marko


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /