[Python] Why I need the parameter when the call doesn't use it?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Aug 29 00:40:38 EDT 2011


On Mon, 29 Aug 2011 12:34 pm Ben Finney wrote:

> Chris Gonnerman <chris at gonnerman.org> writes:
> 
>> On 08/28/2011 07:26 PM, Niklas Rosencrantz wrote:
>> > class A(BaseHandler, blobstore_handlers.BlobstoreUploadHandler):
>> >      def is_submitter_human(self):
> 
>> is_submitter_human() isn't a function, it's a method.
> 
> No, that's not true and may lead to future confusion.
> 
> Rather, it is a function *and* a method. Not all functions are methods,
> but all methods are functions.

Wouldn't it be more accurate to say that methods *wrap* functions?

>>> class C(object):
...     def spam(self):
...             pass
...
>>> C().spam
<bound method C.spam of <__main__.C object at 0xb7e975cc>>
>>> C().spam.im_func
<function spam at 0xb7e8c25c>


(At least for pure Python methods... those written in C, such as for the
built-in types, don't.)


>> Methods are always called with a reference to the class instance
> 
> Also not true, but perhaps too subtle an issue to explore in this thread.

But for the record, you have "normal" instance methods, class methods,
static methods, and any other sort of method you can create using the
descriptor protocol, such as this one:

http://code.activestate.com/recipes/577030-dualmethod-descriptor/

But as Ben hints at, this is getting into fairly advanced territory.



-- 
Steven




More information about the Python-list mailing list