[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Method / Functions - What are the differences?

Michael Rudolf

2/28/2010 12:38:00 PM

Out of curiosity I tried this and it actually worked as expected:

>>> class T(object):
x=[]
foo=x.append
def f(self):
return self.x


>>> t=T()
>>> t.f()
[]
>>> T.foo(1)
>>> t.f()
[1]
>>>

At first I thought "hehe, always fun to play around with python. Might
be useful sometimes" - but then It really confused me what I did. I
mean: f is what we call a method, right? But was is foo? It is not a
method and not a classmethod as it accepts no self and no cls.
So that leaves staticmethod? OK, fair, as x is "static" here anyway this
reflects what it does. But then consider this:

>>> class T(object):
def __init__(self):
self.x=[]
self.foo=self.x.append
def f(self):
return self.x


>>> y=T()
>>> y.x
[]
>>> y.foo(1)
>>> y.x
[1]
>>> a=T()
>>> a.x
[]
>>> a.foo(2)
>>> a.x
[2]
>>>

Note that all I did was moving the list and foo into the instance. Still
no self and no cls, but also no static behaviour any more.

So is foo just nothing of the above and really only a class/instance
attribute which happens to be callable?

Perhaps this all does not matter, but now I am really confused about the
terminology. So: what makes a method a method? And of what type?

Regards,
Michael
29 Answers

Alf P. Steinbach

2/28/2010 2:09:00 PM

0

* Michael Rudolf:
> Out of curiosity I tried this and it actually worked as expected:
>
> >>> class T(object):
> x=[]
> foo=x.append
> def f(self):
> return self.x
>
>
> >>> t=T()
> >>> t.f()
> []
> >>> T.foo(1)
> >>> t.f()
> [1]
> >>>
>
> At first I thought "hehe, always fun to play around with python. Might
> be useful sometimes" - but then It really confused me what I did. I
> mean: f is what we call a method, right? But was is foo?

foo is (refers to) an object that supports call notation and that forwards calls
somewhere else, in this case to append on a list.

You might call it (descriptive) a call forwarder, or (C# or general terminology)
a delegate, or (Python 2.x) a bound method.


<example>
>>> "Hello".upper
<built-in method upper of str object at 0x00BA16E0>
>>> f = "Hello".upper
>>> f
<built-in method upper of str object at 0x00BA16E0>
>>> f()
'HELLO'
>>>
>>>
>>>
>>> f.__self__
'Hello'
>>> f.__call__
<method-wrapper '__call__' of builtin_function_or_method object at 0x00BDD170>
>>> print( f.__doc__ )
S.upper() -> str

Return a copy of S converted to uppercase.
>>> _
</example>


A common use for delegates is as command handlers in a GUI application, and in
general for event notifications.


Cheers & hth.,

- Alf

Rob Williscroft

2/28/2010 2:24:00 PM

0

Michael Rudolf wrote in news:hmdo3m$287$1@news.urz.uni-heidelberg.de in
comp.lang.python:

> Note that all I did was moving the list and foo into the instance. Still
> no self and no cls, but also no static behaviour any more.

Yes in the first case foo was an attribute of the class, and in the second
an attribute of aon instance of the class.

In both cases it was a bound method, something similar too:

lambda item : T.x.append( item )

Michael Rudolf

2/28/2010 4:39:00 PM

0

Am 28.02.2010 15:08, schrieb Alf P. Steinbach:
> >>> "Hello".upper
> <built-in method upper of str object at 0x00BA16E0>
> >>> f = "Hello".upper
> >>> f
> <built-in method upper of str object at 0x00BA16E0>
> >>> f()
> 'HELLO'
> >>>
> >>>
> >>>
> >>> f.__self__
> 'Hello'

Holy hand grenade.
You have no Idea how enlightened I feel right now :D

Thank you, "bound method" was the term I forgot and your example...
....totally revealed the internals behind this to me. Especially the last
line I quoted.
I mean, I always knew *that* this works, but I never knew *why*.

Regards,
Michael

Bruno Desthuilliers

3/1/2010 8:00:00 PM

0

Michael Rudolf a écrit :
> Out of curiosity I tried this and it actually worked as expected:
>
>>>> class T(object):
> x=[]
> foo=x.append
> def f(self):
> return self.x
>
>
>>>> t=T()
>>>> t.f()
> []
>>>> T.foo(1)
>>>> t.f()
> [1]
>>>>
>
> At first I thought "hehe, always fun to play around with python. Might
> be useful sometimes" - but then It really confused me what I did. I
> mean: f is what we call a method, right?

Wrong. It's a function. T.f is an unbound method (in python 2.x at
least) and t.f is a bound method.

> But was is foo?

A bound method. Bound to x, of course.

> It is not a
> method and not a classmethod as it accepts no self and no cls.

Yes it does. Else how would t.foo(4) (or T.foo(4)) append 4 to x ?

> Perhaps this all does not matter,

It does.

> but now I am really confused about the
> terminology. So: what makes a method a method?

The right question is: what makes a function a method !-)

> And of what type?

Answer here:

http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/bd71264b6022765c/3a77541bf9d6617d#doc_89d608...

I really have to put this in the wiki :-/

John Posner

3/2/2010 1:08:00 AM

0

On 3/1/2010 2:59 PM, Bruno Desthuilliers wrote:

> Answer here:
>
> http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/bd71264b6022765c/3a77541bf9d6617d#doc_89d608...
>
> I really have to put this in the wiki :-/


Bruno, I performed a light copy-edit of your writeup and put in some
reStructuredText (reST) markup. The result is at:

http://cl1p.net/bruno...

The only sentence that I think needs work is:

Having access to itself (of course), the
instance (if there's one) and the class, it's easy for it
to wrap all this into a **method** object.

Maybe this?

With the instance object (if any) and class object available,
it's easy to create a method object that wraps the function object.


Begging pardon for my presumptuousness,
John

Bruno Desthuilliers

3/2/2010 8:58:00 AM

0

John Posner a écrit :
> On 3/1/2010 2:59 PM, Bruno Desthuilliers wrote:
>
>> Answer here:
>>
>> http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/bd71264b6022765c/3a77541bf9d6617d#doc_89d608...
>>
>>
>> I really have to put this in the wiki :-/
>
>
> Bruno, I performed a light copy-edit of your writeup and put in some
> reStructuredText (reST) markup. The result is at:
>
> http://cl1p.net/bruno...

Cool.

>
> The only sentence that I think needs work is:
>
> Having access to itself (of course), the
> instance (if there's one) and the class, it's easy for it
> to wrap all this into a **method** object.
>
> Maybe this?
>
> With the instance object (if any) and class object available,
> it's easy to create a method object that wraps the function object.

That's perfect.

But there's also a typo to fix in the Python implementation of the
Method object: in the call method, it should inject self.im_self as
first arg, not self.im_func. This had been spotted by someone named John
Posner, IIRC !-)


>
> Begging pardon for my presumptuousness,

Begging pardon for my laziness :-/

John Posner

3/2/2010 2:17:00 PM

0

On 3/2/2010 3:57 AM, Bruno Desthuilliers wrote:
>>
>> With the instance object (if any) and class object available,
>> it's easy to create a method object that wraps the function object.
>
> That's perfect.


Fixed.

> But there's also a typo to fix in the Python implementation of the
> Method object: in the call method, it should inject self.im_self as
> first arg, not self.im_func. This had been spotted by someone named John
> Posner, IIRC !-)


Fixed (oops!).

I've updated the text at this location:

> http://cl1p.net/bruno...

I think the ball is back in your court, Bruno. I'd be happy to help more
-- feel free to contact me off-list, at jjposner@optimum.net.

Best,
John

Eike Welk

3/2/2010 10:00:00 PM

0

John Posner wrote:
> I've updated the text at this location:
>
> > http://cl1p.net/bruno...

I think this is a very useful writeup!

It would be perfect with a little bit of introduction that says:
1. - What it is: "The rough details of method look-up";
2. - which contains some of the questions that that made that authors write
the text. This way people with similar questions can find it with Google.

Additionally the link to the relevant section in the Python documentation
would be great. I can't find it!

A link to an article about the details of class creation and metaclasses
would be good too.


Thanks for writing this great little text,
Eike.

Bruno Desthuilliers

3/3/2010 10:56:00 AM

0

Eike Welk a écrit :
> John Posner wrote:
>> I've updated the text at this location:
>>
>> > http://cl1p.net/bruno...
>
> I think this is a very useful writeup!
>
> It would be perfect with a little bit of introduction that says:
> 1. - What it is: "The rough details of method look-up";
> 2. - which contains some of the questions that that made that authors write
> the text. This way people with similar questions can find it with Google.
>

John, do you think you could something with the following ?

"""
"Is it a function ? is it a method ? No, it's... " - or : What's in a
Python method ?

Python newcomers often have hard time understanding the "magic" behind
Python's methods - and truth is that Python's object model can be a bit
peculiar when compared to most mainstream (or not-so-mainstream) OOPLs.
As a matter of fact, there are quite a few threads on c.l.py with either
direct or indirect questions about what makes a Python method, and I
must have explained the whole mechanism at least 3 or 4 times there. The
following text is an edited version of my last attempt, as edited,
corrected and published by John Posner, MayHisNameBePraised(tm).

This text isn't meant as a replacement for neither the official
FineManual(tm)[XXX : relevant link] nor the very excellent - if somehow
technical - 'Descriptors how-to' [XXX : relevant link]. It's mostly a
brief but hopefully helpful overview of what exactly is a Python method,
and how Python magically inserts the 'self' or 'cls' argument to method
calls.
"""

Feel free to edit / amend / rewrite / trash at will - you're now
officially in charge of publishing this text !-)

John Posner

3/3/2010 2:59:00 PM

0

On 3/3/2010 5:56 AM, Bruno Desthuilliers wrote:
> Eike Welk a écrit :
>> John Posner wrote:
>>> I've updated the text at this location:
>>>
>>> > http://cl1p.net/bruno...
>>
>> I think this is a very useful writeup!
>> It would be perfect with a little bit of introduction that says:
>> 1. - What it is: "The rough details of method look-up";
>> 2. - which contains some of the questions that that made that authors
>> write the text. This way people with similar questions can find it
>> with Google.
>>
>
> John, do you think you could something with the following ?

Sure thing, Bruno. I'll incorporate/edit your new text below into a
Python Wiki entry. The headings in the Documentation table of contents
page (http://wiki.python.org/moin/Doc...) seem pretty sober, so I
plan to use a straightforward title:

FromFunctionsToMethods

.... instead of the clearly superior ...

ItsAFunctionItsAMethodItsAUseOfTheDescriptorProtocol

Does this article belong in the "Advanced Topics" section of the page?
I'm not sure, but I'll place it there for now. (Alternative suggestions
welcome!)

>
> """
> "Is it a function ? is it a method ? No, it's... " - or : What's in a
> Python method ?
>
> Python newcomers often have hard time understanding the "magic" behind
> Python's methods - and truth is that Python's object model can be a bit
> peculiar when compared to most mainstream (or not-so-mainstream) OOPLs.
> As a matter of fact, there are quite a few threads on c.l.py with either
> direct or indirect questions about what makes a Python method, and I
> must have explained the whole mechanism at least 3 or 4 times there. The
> following text is an edited version of my last attempt, as edited,
> corrected and published by John Posner, MayHisNameBePraised(tm).
>
> This text isn't meant as a replacement for neither the official
> FineManual(tm)[XXX : relevant link] nor the very excellent - if somehow
> technical - 'Descriptors how-to' [XXX : relevant link]. It's mostly a
> brief but hopefully helpful overview of what exactly is a Python method,
> and how Python magically inserts the 'self' or 'cls' argument to method
> calls.
> """
>
> Feel free to edit / amend / rewrite / trash at will - you're now
> officially in charge of publishing this text !-)
>

Yow, the mantle of responsibility weighs heavily upon my poor shoulders!

Film at 11,
John