[Tutor] Where's the Walrus?

Remco Gerlich scarblac@pino.selwerd.nl
Wed, 21 Feb 2001 08:01:58 +0100


On Tue, Feb 20, 2001 at 06:38:22PM -0900, 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:

Two simple mistakes:

> #######################################################
> 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
> 	def next_personality():

You forgot the argument "self" here.

> 		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

And here you merely mention the method, but don't call it; add () to the end.

> print ally_oop

-- 
Remco Gerlich