Python 3K or Python 2.9?

Terry Reedy tjreedy at udel.edu
Fri Sep 14 02:42:03 EDT 2007


"Bjoern Schliessmann" <usenet-mail-0306.20.chr0n0ss at spamgourmet.com> wrote 
in message news:5ktre7F5ig0vU1 at mid.individual.net...
|> - only functions being attributes of a class...

|What, IYHO, is the difference between a method and a function?

A method is a function accessed as an attribute of a class or instance.
As an object type, it is a *runtime* function wrapper.

|> (ok, I know, you meant "functions declared within a class> statement").

| I think that those functions _are_ special ones

Thinking does not make things so.

|since the compiler is able to make "method(instance, a, b)" out of
|"instance.method(a, b)".

No it does not.  The method wrapping is done at runtine.  The compiler is 
ignorant of the wrapping that will be done.

>>> class C:
 def meth(self): pass

>>> c = C()
>>> import dis
>>> def f(): return c.meth()

>>> dis.dis(f)
  1           0 LOAD_GLOBAL              0 (c)
              3 LOAD_ATTR                1 (meth)
              6 CALL_FUNCTION            0
              9 RETURN_VALUE

The function gets wrapped as a bound method as part of LOAD_ATTR.  When the 
compiler sees <expr>(args), it does not know and does not care about the 
particular type that <expr> will become.  It just assumes that it will be 
callable and emits the code to call it.  Consider

>>> def g(): return C()

>>> dis.dis(g)
  1           0 LOAD_GLOBAL              0 (C)
              3 CALL_FUNCTION            0
              6 RETURN_VALUE

It does not notice and does not care that 'C' will be bound to a class.

Terry Jan Reedy








More information about the Python-list mailing list