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