[Tutor] Where's the Walrus?

Glen Wheeler wheelege@tsn.cc
Wed, 21 Feb 2001 18:35:11 +1100


This is a multi-part message in MIME format.

------=_NextPart_000_0043_01C09C35.05D02260
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


  Howdy how,

  Just two little errors I can see....read on :)

----- Original Message -----
From: Tim Johnson <tim@johnsons-web.com>
To: <tutor@python.org>
Sent: Wednesday, February 21, 2001 2:38 PM
Subject: [Tutor] Where's the Walrus?


> Hello:
> I'm creating my first object.
> I have a method to advance an index.
> The method doesn't respond.
> code is below:
> #######################################################
> class schiz:
> def __init__(self):
> self.moods =3D ["I'm a little teapot", "I am the Walrus",
>               "I hate broccoli", "I'm the Jolly Green Giant"]
> self.which =3D 0
>             # my code doesn't even get called, or so it appears
> def next_personality(self):  ## you need to have it pass self to every
single function inside a class
> print "next personality"
> if self.which < 4:
> self.which =3D self.which + 1
> print "next"
> else:
> self.which =3D 0
> print "first"
> def __str__(self):
> return self.moods[self.which]
> def __repr__(self):
> return self.moods[self.which]
> def __call__(self):
> return self.next_personality()
> ally_oop =3D schiz()
> print ally_oop
> ally_oop.next_personality() ## you had no brackets - that does not =
call
the function.  Same as if you put brackets in a binding it would call =
it,
not having brackets 'assigns' it.
> print ally_oop
> #######################################################
> FYI: Am coming from a background in C++
> but please don't hold that against me :)
> TIA
> --
> Tim Johnson
> -----------
> "Of all manifestations of power,
>  restraint impresses the most."
>  -Thucydides
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor



  Then it works like a charm :)


------=_NextPart_000_0043_01C09C35.05D02260
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 content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><BR>&nbsp; Howdy how,<BR><BR>&nbsp; Just two little errors I can=20
see....read on :)<BR><BR>----- Original Message -----<BR>From: Tim =
Johnson=20
&lt;<A =
href=3D"mailto:tim@johnsons-web.com">tim@johnsons-web.com</A>&gt;<BR>To: =

&lt;<A =
href=3D"mailto:tutor@python.org">tutor@python.org</A>&gt;<BR>Sent:=20
Wednesday, February 21, 2001 2:38 PM<BR>Subject: [Tutor] Where's the=20
Walrus?<BR><BR><BR>&gt; Hello:<BR>&gt; I'm creating my first =
object.<BR>&gt; I=20
have a method to advance an index.<BR>&gt; The method doesn't =
respond.<BR>&gt;=20
code is below:<BR>&gt;=20
#######################################################<BR>&gt; class=20
schiz:<BR>&gt; def __init__(self):<BR>&gt; self.moods =3D ["I'm a little =
teapot",=20
"I am the=20
Walrus",<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;=20
"I hate broccoli", "I'm the Jolly Green Giant"]<BR>&gt; self.which =3D=20
0<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
# my code doesn't even get called, or so it appears<BR>&gt; def=20
next_personality(self):&nbsp; ## you need to have it pass self to=20
every<BR>single function inside a class<BR>&gt; print "next =
personality"<BR>&gt;=20
if self.which &lt; 4:<BR>&gt; self.which =3D self.which + 1<BR>&gt; =
print=20
"next"<BR>&gt; else:<BR>&gt; self.which =3D 0<BR>&gt; print =
"first"<BR>&gt; def=20
__str__(self):<BR>&gt; return self.moods[self.which]<BR>&gt; def=20
__repr__(self):<BR>&gt; return self.moods[self.which]<BR>&gt; def=20
__call__(self):<BR>&gt; return self.next_personality()<BR>&gt; ally_oop =
=3D=20
schiz()<BR>&gt; print ally_oop<BR>&gt; ally_oop.next_personality() ## =
you had no=20
brackets - that does not call<BR>the function.&nbsp; Same as if you put =
brackets=20
in a binding it would call it,<BR>not having brackets 'assigns' =
it.<BR>&gt;=20
print ally_oop<BR>&gt;=20
#######################################################<BR>&gt; FYI: Am =
coming=20
from a background in C++<BR>&gt; but please don't hold that against me=20
:)<BR>&gt; TIA<BR>&gt; --<BR>&gt; Tim Johnson<BR>&gt; =
-----------<BR>&gt; "Of=20
all manifestations of power,<BR>&gt;&nbsp; restraint impresses the=20
most."<BR>&gt;&nbsp; -Thucydides<BR>&gt;<BR>&gt;=20
_______________________________________________<BR>&gt; Tutor =
maillist&nbsp;=20
-&nbsp; <A href=3D"mailto:Tutor@python.org">Tutor@python.org</A><BR>&gt; =
<A=20
href=3D"http://mail.python.org/mailman/listinfo/tutor">http://mail.python=
.org/mailman/listinfo/tutor</A><BR><BR><BR><BR>&nbsp;=20
Then it works like a charm :)<BR></DIV></BODY></HTML>

------=_NextPart_000_0043_01C09C35.05D02260--