[Tutor] What does "sort" without parens do?

Gonçalo Rodrigues op73418@mail.telepac.pt
Mon Dec 16 15:12:14 2002


This is a multi-part message in MIME format.

------=_NextPart_000_0018_01C2A540.38E5A7E0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hmm, what I meant with my reply was exactly what you said "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." My =
appologies if my poor english was unable to render the intended meaning.

With my best regards,
G. Rodrigues

P.S: And as I am in an asking-appologies mood I might as well apologize =
for top-posting. Outlook is giving me a bad time. Must ditch it...

  ----- Original Message -----=20
  From: Poor Yorick=20
  To: Gon=E7alo Rodrigues=20
  Cc: Terry Carroll ; tutor@python.org=20
  Sent: Monday, December 16, 2002 3:10 PM
  Subject: Re: [Tutor] What does "sort" without parens do?


  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 =3D [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]
  >>>=20

  Poor Yorick
  gp@pooryorick.com

  Gon=E7alo Rodrigues wrote:

----- Original Message -----From: "Terry Carroll" <carroll@tjc.com>To: =
<tutor@python.org>Sent: Friday, December 13, 2002 4:55 PMSubject: =
[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 =
MP3collection).To sort the list: I mistyped:  file_list.sortOkay, 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'snot supposed to.  But I'm intrigued that it didn't =
give a syntax error orexception, or, as far as I can tell, have any =
effect at all.  How didPython interpret that line?
In Python *everything* is an object - In particular functions, methods, =
etcare objects with the same status and privelieges than others (like =
lists).When Python sees something likefile_list.sortIt 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_listand finds a =
sort attribute there. It then returns what it found with somewrapping to =
make it a bound method - I'm being a little bit sloppy, but itdoen't =
matter. You could for example dosorting_file_list =3D file_list.sortNow =
sorting_file_list  is what is called a callable - it behaves much like =
afunction. 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 =
syntacticlevel you "call" it by sorting_file_list() - notice the =
parenthesis? It is*exactly the same* as if you had=20
done file_list.sort() directly.
--Terry Carroll
HTH,G. Rodrigues_______________________________________________Tutor =
maillist  -  =
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor



------=_NextPart_000_0018_01C2A540.38E5A7E0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2716.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hmm, what I meant with my reply was =
exactly what=20
you said "<FONT face=3D"Times New Roman" size=3D3>When Python sees a =
function=20
without the ensuing parenthesis, it looks up the function in the =
appropriate=20
namespaces, and if it finds the function, continues through the program =
without=20
calling the function.</FONT>" My appologies if my poor english was =
unable to=20
render the intended meaning.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>With my best regards,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>G. Rodrigues</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>P.S: And as I am in an =
asking-appologies mood I=20
might as well apologize for top-posting. Outlook is giving me a bad =
time. Must=20
ditch it...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Dgp@pooryorick.com href=3D"mailto:gp@pooryorick.com">Poor =
Yorick</A>=20
  </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dop73418@mail.telepac.pt=20
  href=3D"mailto:op73418@mail.telepac.pt">Gon=E7alo Rodrigues</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Cc:</B> <A title=3Dcarroll@tjc.com=20
  href=3D"mailto:carroll@tjc.com">Terry Carroll</A> ; <A =
title=3Dtutor@python.org=20
  href=3D"mailto:tutor@python.org">tutor@python.org</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Monday, December 16, 2002 =
3:10=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: [Tutor] What does =
"sort"=20
  without parens do?</DIV>
  <DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial=20
  size=3D2></FONT><BR></DIV>G. Rodrigues' answer to this question seems =
to say=20
  that sort is still being called even without the parenthesis. &nbsp;I =
don't=20
  think this is the case. &nbsp;When Python sees a function without the =
ensuing=20
  parenthesis, it looks up the function in the appropriate namespaces, =
and if it=20
  finds the function, continues through the program without calling the=20
  function. &nbsp;Therefore, a command =
like:<BR><BR>file_list.sort<BR><BR>does=20
  nothing at all.<BR><BR>&gt;&gt;&gt; list1 =3D [9, 7, 3, 5, 2, 8,=20
  1]<BR>&gt;&gt;&gt; list1.sort<BR>&lt;built-in method sort of list =
object at=20
  0x00915790&gt;<BR>&gt;&gt;&gt; list1<BR>[9, 7, 3, 5, 2, 8, =
1]<BR>&gt;&gt;&gt;=20
  <BR><BR>Poor Yorick<BR><A class=3Dmoz-txt-link-abbreviated=20
  =
href=3D"mailto:gp@pooryorick.com">gp@pooryorick.com</A><BR><BR>Gon=E7alo =
Rodrigues=20
  wrote:<BR>
  <BLOCKQUOTE cite=3Dmid:003301c2a2cb$2f8c4d60$a9160dd5@violante =
type=3D"cite"><PRE wrap=3D"">----- Original Message -----<BR>From: =
"Terry Carroll" <A class=3Dmoz-txt-link-rfc2396E =
href=3D"mailto:carroll@tjc.com">&lt;carroll@tjc.com&gt;</A><BR>To: <A =
class=3Dmoz-txt-link-rfc2396E =
href=3D"mailto:tutor@python.org">&lt;tutor@python.org&gt;</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=3D"cite"><PRE wrap=3D"">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=3D""><!----><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 =3D =
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=20
done file_list.sort() directly.<BR><BR></PRE>
    <BLOCKQUOTE type=3D"cite"><PRE wrap=3D"">--<BR>Terry =
Carroll<BR></PRE></BLOCKQUOTE><PRE wrap=3D""><!----><BR>HTH,<BR>G. =
Rodrigues<BR><BR><BR><BR>_______________________________________________<=
BR>Tutor maillist  -  <A class=3Dmoz-txt-link-abbreviated =
href=3D"mailto:Tutor@python.org">Tutor@python.org</A><BR><A =
class=3Dmoz-txt-link-freetext =
href=3D"http://mail.python.org/mailman/listinfo/tutor">http://mail.python=
.org/mailman/listinfo/tutor</A><BR><BR><BR></PRE></BLOCKQUOTE><BR></BLOCK=
QUOTE></BODY></HTML>

------=_NextPart_000_0018_01C2A540.38E5A7E0--