Python 3K or Python 2.9?

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Sep 14 03:17:48 EDT 2007


Bjoern Schliessmann a écrit :
> Bruno Desthuilliers wrote:
>> Bjoern Schliessmann a écrit :
> 
>>> Why don't you make a preprocessor which accepts method
>>> declarations without "self" and fixes them?
>> The problem being that there's no such thing as a "method
>> declaration" in Python 
> 
> Yep, there are only definitions. I'm sorry.
> 
>> - only functions being attributes of a class...
> 
> What, IYHO, is the difference between a method and a function?

A method is a thin wrapper around a function, usually instanciated and 
returned by the __get__ method [1] of the function itself when the 
function is looked up as an attribute of a class or an instance:

 >>> class Foo(object):
...     def meth(self):
...             print "in %s meth" % self
...
 >>> Foo.__dict__['meth']
<function meth at 0xb7d76374>
 >>> Foo.meth
<unbound method Foo.meth>
 >>> Foo().meth
<bound method Foo.meth of <__main__.Foo object at 0xb7d7acac>>
 >>>

[1] you may want to read about the descriptor protocol which is the base 
mechanism on which methods and properties (computed attributes) are based.

>> (ok, I know, you meant "functions declared within a class
>> statement").
> 
> I think that those functions _are_ special ones

They aren't, and you should perhaps read the manual - all this is 
documented.

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

Once again, the compiler has *nothing* to do with this. Else, how could 
you do this:

 >>> def fun(obj):
...     print obj
...
 >>> Foo.fun = fun
 >>> Foo.fun
<unbound method Foo.fun>
 >>> Foo().fun
<bound method Foo.fun of <__main__.Foo object at 0xb7d7ad6c>>
 >>> fun
<function fun at 0xb7d763ac>
 >>> Foo.__dict__['fun']
<function fun at 0xb7d763ac>
 >>>


> So IMHO, "method definition" makes sense.

It doesn't.



More information about the Python-list mailing list