confused about the different built-in functions in Python

Marko Rauhamaa marko at pacujo.net
Mon May 26 16:58:37 EDT 2014


Marko Rauhamaa <marko at pacujo.net>:

> Christian Heimes <christian at python.org>:
>
>> Python creates a new bound method object every time. A bound method
>> object is a callable object that keeps a strong reference to the
>> function, class and object. The bound method object adds the object as
>> first argument to the function (aka 'self').
>
> I stand corrected. I had thought the trampoline ("bound method
> object") was created once and for all.

Sure enough. The principle is explicitly specified in <URL:
https://docs.python.org/3.2/reference/datamodel.html#index-46>.

Thus:

   >>> class X:
   ...   def f(self):
   ...     print("Hello")
   ... 
   >>> x = X()
   >>> x.f()
   Hello
   >>> def f(): print("Meh")
   ... 
   >>> x.f = f
   >>> x.f()
   Meh
   >>> delattr(x, "f")
   >>> x.f()
   Hello

IOW, you can override a method with setattr() but you cannot delete a
method with delattr().


Marko



More information about the Python-list mailing list