[Tutor] Where's the Walrus?

Jose Amoreira amoreira@mercury.ubi.pt
Wed, 21 Feb 2001 10:16:40 +0000


Hello !
It's just a typo! You frogot the 'self' argument in 'def next_personality()'!




Tim Johnson wrote:

> 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 = ["I'm a little teapot", "I am the Walrus",
>                               "I hate broccoli", "I'm the Jolly Green Giant"]
>                 self.which = 0
>             # my code doesn't even get called, or so it appears

# Don't forget the 'self' argument in class methods definitions!!!!

>
>         def next_personality():
>                 print "next personality"
>                 if self.which < 4:
>                         self.which = self.which + 1
>                         print "next"
>                 else:
>                         self.which = 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 = schiz()
> print ally_oop
> ally_oop.next_personality
> 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