[Edu-sig] Getting ready for class...
Gregor Lingl
glingl at aon.at
Fri Mar 10 00:20:31 CET 2006
kirby urner schrieb:
> Here's what I'm starting with today:
>
> http://www.4dsolutions.net/ocn/python/zoo.py
>
> Note: inheriting from object at the top level, per André's suggestion.
>
> Kirby
>
I also like this example very much (especially the somewhat
sophisticated use of __repr__).
Despite of the lack of echo to my former (turtle-)example I'd like to
contribute one more - this one without turtles (the English translation
may be a bit inept):
In my class I let the students do the following exercise to sum up or
recapitulate what they have learned in previous lessons about objects,
inheritance and message passing:
We begin very simply (who wants may inherit from object):
>>> class Messenger:
def announce(self):
print "I announce to you:", "Hello!"
>>> herold = Messenger()
>>> herold.announce()
I announce to you: Hello!
>>> annunciator = Messenger()
>>> annunciator.announce()
I announce to you: Hello!
>>>
#### Not very diversified ... We improve it:
>>> class Messenger:
def announce(self, text):
print "I announce to you:", text
>>> herold = Messenger()
>>> herold.announce("Fire on the roof")
I announce to you: Fire on the roof
>>> herold.announce("Hurricane approaching!")
I announce to you: Hurricane approaching!
>>>
#### Now we want the messengers to remember their message text:
>>> class CleverMessenger:
def note(self, text):
self.message = text
def announce(self):
print "I announce to you:", self.message
>>> wiseguy = CleverMessenger()
>>> wiseguy.note("Shares are rising!")
>>> wiseguy.announce()
I announce to you: Shares are rising!
>>> vifzac = CleverMessenger()
>>> vifzac.note("Weather is becoming cold!")
>>> vifzac.announce()
I announce to you: Weather is becoming cold!
>>>
>>> vifzac.note("Weather is becoming warm!")
>>> vifzac.announce()
I announce to you: Weather is becoming warm!
>>> vifzac.message
'Weather is becoming warm!'
#### ( A look at an attribute )
#### Now inheritance: A VeryCleverMessenger 'isa' CleverMessenger
>>> class VeryCleverMessenger(CleverMessenger):
def note_additionally(self, addendum):
self.message = self.message+" "+addendum
>>> angelo = VeryCleverMessenger()
>>> angelo.note("It's becoming cold!")
>>> angelo.announce()
I announce to you: It's becoming cold!
>>> angelo.note_additionally("Use warm clothing!")
>>> angelo.announce()
I announce to you: It's becoming cold! Use warm clothing!
>>> merkur = CleverMessenger()
>>> merkur.note("Traffic jam on all Highways!")
>>> merkur.note_additionally("Defer trip!")
Traceback (most recent call last):
File "<pyshell#44>", line 1, in -toplevel-
merkur.note_additionally("Defer trip!")
AttributeError: CleverMessenger instance has no attribute
'note_additionally'
#### We see: merkur is not very clever
#### The following shows a serious defect of our messenger classes
>>> zweistein = VeryCleverMessenger()
>>> zweistein.announce()
I announce to you:
Traceback (most recent call last):
File "<pyshell#46>", line 1, in -toplevel-
zweistein.announce()
File "<pyshell#20>", line 5, in announce
print "I announce to you:", self.message
AttributeError: VeryCleverMessenger instance has no attribute 'message'
#### We will repair this by initializing the message attribute:
>>> class CleverMessenger:
def __init__(self):
self.message="Hi!"
def note(self, text):
self.message = text
def announce(self):
print "I announce to you:", self.message
>>> class VeryCleverMessenger(CleverMessenger):
def note_additionally(self, addendum):
self.message = self.message+" "+addendum
>>> zweistein = VeryCleverMessenger()
>>> zweistein.announce()
I announce to you: Hi!
#### One more inheritance example:
>>> class FriendlyMessenger(CleverMessenger):
def __init__(self, greeting):
CleverMessenger.__init__(self)
self.greeting = greeting
def greet(self):
print self.greeting
>>> sunny = FriendlyMessenger("Have a nice day!")
>>> sunny.greet()
Have a nice day!
>>> sunny.announce()
I announce to you: Hi!
#### Overwrite methods
>>> class FriendlyMessenger(CleverMessenger):
def __init__(self, greeting):
CleverMessenger.__init__(self)
self.greeting = greeting
def greet(self):
print self.greeting
def announce(self):
self.greet()
CleverMessenger.announce(self)
>>> sunny = FriendlyMessenger("Have a nice day!")
>>> sunny.announce()
Have a nice day!
I announce to you: Hi!
>>> sunny.note("OOP is pretty useful!")
>>> sunny.announce()
Have a nice day!
I announce to you: OOP is pretty useful!
>>> tim = CleverMessenger()
>>> tim.note("Tomorrow it will happen!")
>>> tim.announce()
I announce to you: Tomorrow it will happen!
#### Last example: communicating objects
>>> class Agent(CleverMessenger):
def passMessage(self, agent):
agent.note(self.message)
def listen(self, agent):
self.note(agent.message)
>>> james = Agent()
>>> austin = Agent()
>>> james.announce()
I announce to you: Hi!
>>> james.listen(tim)
>>> james.announce()
I announce to you: Tomorrow it will happen!
>>> austin.announce()
I announce to you: Hi!
>>> james.passMessage(austin)
>>> austin.announce()
I announce to you: Tomorrow it will happen!
>>>
I assume that this interactive session is somewhat selfexplanatory, so I
omit additional explanations.
Regards,
Gregor
More information about the Edu-sig
mailing list