[Tutor] Translate this VB.NET code into Python for me?
Jerry Hill
malaclypse2 at gmail.com
Tue Mar 4 16:10:50 CET 2008
On Tue, Mar 4, 2008 at 8:05 AM, Dick Moores <rdm at rcblue.com> wrote:
> By the second chapter I've begun to suspect that GUIs aside, Python
> is a lot simpler to write. Could someone prove that to me by
> translating the code I've pasted at
> <http://py77.python.pastebin.com/f24d74b17> (from pp. 51-54 of the
> book), which prints
This code is pretty straightforward to translate into python. It's
pretty short so, I've pasted it inline:
import sys
class Dog(object):
def __init__(self, name, sound="barks"):
self.name = name
self.sound = sound
def bark(self):
print "%s %s" % (self.name, self.sound)
def do_your_thing(self):
raise NotImplementedError
class Rottweiler(Dog):
def do_your_thing(self):
print "%s snarls at you in a menacing fashion." % self.name
class Spaniel(Dog):
def do_your_thing(self):
print "%s licks you all over, then drools on you." % self.name
if __name__ == "__main__":
butch = Rottweiler("Butch")
mac = Spaniel("Mac", "yips")
butch.bark()
mac.bark()
butch.do_your_thing()
mac.do_your_thing()
sys.stdin.readline()
It's probably more pythonic to not define the Dog.do_your_thing()
method at all than to raise the NotImplementedError, but I think that
this way mirrors the VB code a bit better. I don't think there's a
good way to mark the entire Dog class as abstract in python, which I
think is what the VB code does with the "MustInherit Class Dog" line.
--
Jerry
More information about the Tutor
mailing list