[Tutor] What does "sort" without parens do?
Poor Yorick
gp@pooryorick.com
Mon Dec 16 10:10:06 2002
--------------080705090202090402000608
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
G. Rodrigues' answer to this question seems to say that sort is still
being called even without the parenthesis. I don't think this is the
case. When Python sees a function without the ensuing parenthesis, it
looks up the function in the appropriate namespaces, and if it finds the
function, continues through the program without calling the function.
Therefore, a command like:
file_list.sort
does nothing at all.
>>> list1 = [9, 7, 3, 5, 2, 8, 1]
>>> list1.sort
<built-in method sort of list object at 0x00915790>
>>> list1
[9, 7, 3, 5, 2, 8, 1]
>>>
Poor Yorick
gp@pooryorick.com
Gonçalo Rodrigues wrote:
>----- Original Message -----
>From: "Terry Carroll" <carroll@tjc.com>
>To: <tutor@python.org>
>Sent: Friday, December 13, 2002 4:55 PM
>Subject: [Tutor] What does "sort" without parens do?
>
>
>>I made an error in a program, and Python's handling of it befuddles me.
>>I had a list named "file_list" that was full of instances of a class,
>>each instance describing a file on a set of CDROMs (I'm indexing my MP3
>>collection).
>>
>>To sort the list: I mistyped:
>>
>> file_list.sort
>>
>>Okay, that's wrong. I should have used:
>>
>> file_list.sort()
>>
>>I did that, and everything worked fine.
>>
>>Now, I'm not surprised that the version without parens didn't work -- it's
>>not supposed to. But I'm intrigued that it didn't give a syntax error or
>>exception, or, as far as I can tell, have any effect at all. How did
>>Python interpret that line?
>>
>
>In Python *everything* is an object - In particular functions, methods, etc
>are objects with the same status and privelieges than others (like lists).
>When Python sees something like
>
>file_list.sort
>
>It just looks for the attribute sort in the object (a list in this case)
>file_list. Since it can't find it there, it goes to the class of file_list
>and finds a sort attribute there. It then returns what it found with some
>wrapping to make it a bound method - I'm being a little bit sloppy, but it
>doen't matter. You could for example do
>
>sorting_file_list = file_list.sort
>
>Now sorting_file_list is what is called a callable - it behaves much like a
>function. The great difference is that it is a bound method - it "knows"
>what object it applies (the list file_list in this case). At the syntactic
>level you "call" it by sorting_file_list() - notice the parenthesis? It is
>*exactly the same* as if you had done file_list.sort() directly.
>
>>--
>>Terry Carroll
>>
>
>HTH,
>G. Rodrigues
>
>
>
>_______________________________________________
>Tutor maillist - Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
--------------080705090202090402000608
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html>
<head>
</head>
<body>
G. Rodrigues' answer to this question seems to say that sort is still being
called even without the parenthesis. I don't think this is the case. When
Python sees a function without the ensuing parenthesis, it looks up the function
in the appropriate namespaces, and if it finds the function, continues through
the program without calling the function. Therefore, a command like:<br>
<br>
file_list.sort<br>
<br>
does nothing at all.<br>
<br>
>>> list1 = [9, 7, 3, 5, 2, 8, 1]<br>
>>> list1.sort<br>
<built-in method sort of list object at 0x00915790><br>
>>> list1<br>
[9, 7, 3, 5, 2, 8, 1]<br>
>>> <br>
<br>
Poor Yorick<br>
<a class="moz-txt-link-abbreviated" href="mailto:gp@pooryorick.com">gp@pooryorick.com</a><br>
<br>
Gonçalo Rodrigues wrote:<br>
<blockquote type="cite" cite="mid:003301c2a2cb$2f8c4d60$a9160dd5@violante">
<pre wrap="">----- Original Message -----<br>From: "Terry Carroll" <a class="moz-txt-link-rfc2396E" href="mailto:carroll@tjc.com"><carroll@tjc.com></a><br>To: <a class="moz-txt-link-rfc2396E" href="mailto:tutor@python.org"><tutor@python.org></a><br>Sent: Friday, December 13, 2002 4:55 PM<br>Subject: [Tutor] What does "sort" without parens do?<br><br><br></pre>
<blockquote type="cite">
<pre wrap="">I made an error in a program, and Python's handling of it befuddles me.<br>I had a list named "file_list" that was full of instances of a class,<br>each instance describing a file on a set of CDROMs (I'm indexing my MP3<br>collection).<br><br>To sort the list: I mistyped:<br><br> file_list.sort<br><br>Okay, that's wrong. I should have used:<br><br> file_list.sort()<br><br>I did that, and everything worked fine.<br><br>Now, I'm not surprised that the version without parens didn't work -- it's<br>not supposed to. But I'm intrigued that it didn't give a syntax error or<br>exception, or, as far as I can tell, have any effect at all. How did<br>Python interpret that line?<br></pre>
</blockquote>
<pre wrap=""><!----><br>In Python *everything* is an object - In particular functions, methods, etc<br>are objects with the same status and privelieges than others (like lists).<br>When Python sees something like<br><br>file_list.sort<br><br>It just looks for the attribute sort in the object (a list in this case)<br>file_list. Since it can't find it there, it goes to the class of file_list<br>and finds a sort attribute there. It then returns what it found with some<br>wrapping to make it a bound method - I'm being a little bit sloppy, but it<br>doen't matter. You could for example do<br><br>sorting_file_list = file_list.sort<br><br>Now sorting_file_list is what is called a callable - it behaves much like a<br>function. The great difference is that it is a bound method - it "knows"<br>what object it applies (the list file_list in this case). At the syntactic<br>level you "call" it by sorting_file_list() - notice the parenthesis? It is<br>*exactly the same* as if you had
done file_list.sort() directly.<br><br></pre>
<blockquote type="cite">
<pre wrap="">--<br>Terry Carroll<br></pre>
</blockquote>
<pre wrap=""><!----><br>HTH,<br>G. Rodrigues<br><br><br><br>_______________________________________________<br>Tutor maillist - <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a><br><a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br><br><br></pre>
</blockquote>
<br>
</body>
</html>
--------------080705090202090402000608--