[Tutor] Accessing class instances [Party people]
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Mar 18 00:41:30 EST 2004
> But if a person just needs to remember one person, we can just say:
>
> ###
> >>> class SolitaryPerson:
> ... def __init__(self, name):
> ... self.name = name
> ... self.friend = None
> ... def meet(self, other):
> ... self.friend = other
> ... def greet(self):
> ... if self.friend:
> ... print "I", self.name, "greet you,", self.friend.name
> ... else:
> ... print "but I don't have any friends. Waaa."
> ...
> >>> d = SolitaryPerson("Dan")
> >>> d.greet()
> But I Dan don't have any friends. Waaa.
^^^^
Argh. The last example was a bad case of cut-and-paste; the method as
written above won't produce that output. Let me try that again.
###
>>> class SolitaryPerson:
... def __init__(self, name):
... self.name = name
... self.friend = None
... def meet(self, other):
... self.friend = other
... def greet(self):
... if self.friend:
... print "I", self.name, "greet you,", self.friend.name
... else:
... print "but I", self.name, "don't have any friends. Waaa."
...
>>> d = SolitaryPerson("Dan")
>>> d.greet()
but I Dan don't have any friends. Waaa.
>>> d.meet(a)
>>> d.meet(b); d.meet(c) ; d.meet(d)
>>> d.greet()
I Dan greet you, Dan
###
Better, yet sad in a kind of pathetic way. *grin*
My apologies!
More information about the Tutor
mailing list