•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,698 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,526 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
I was looking for a somewhat meaningful and interesting example to explain the Python class to beginners. A room full of multilingual students makes this phrase translator a good choice. You can experiment with it and add more languages and phrases.
# multilingual phrase example to explain a Python class # classes of objects are defined with the keyword class # class names usually start with a capital letter # __init__() method is analogous to a constructor in C++, C# or Java # self identifies the class namespace # myName and self.myName are two different variables # # used AltaVista - Babel Fish Translation at # http://babelfish.altavista.com/ # # ASCII characters above chr(127) are sometimes difficult to display # with some IDEs like DrPython and BOA, use PythonWin or IDLE # # Python24 tested vegaseat 10jun2005 class Person: def __init__(self, myName, language): self.myName = myName self.language = language def sayHi(self): if self.language == 'german': print 'Hallo, mein Name ist', self.myName elif self.language == 'english': print 'Hello, my name is', self.myName elif self.language == 'spanish': print 'Hola, mi nombre es', self.myName else: print 'language name incorrect!' def sayBye(self): if self.language == 'german': print 'Ein herzliches auf Wiedersehen von', self.myName elif self.language == 'english': print 'A hearty goodbye from', self.myName elif self.language == 'spanish': # DaniWeb codefield problem with chr(243) in "adiós" #print 'Un caluroso adios de', self.myName # could use a kludge like this ... print 'Un caluroso adi'+chr(243)+'s de', self.myName else: print 'language name incorrect!' def sayAge(self, age): if self.language == 'german': print self.myName, "ist", age, "Jahre alt" elif self.language == 'english': print self.myName, "is", age, "years old" elif self.language == "spanish": # DaniWeb codefield problem with chr(241) in "años" #print self.myName, "es", age, "anos de viejo" print self.myName, "es", age, "a"+chr(241)+"os de viejo" else: print 'language name incorrect!' print '-'*30 # 30 dashes, cosmetic stuff # you can use this Person('Ronald Smith', 'english').sayHi() print '-'*30 # or better use an alias to save typing and improve readability # (constructs an instance of Person Smith and creates a reference to it in smith) smith = Person('Ronald Smith', 'english') smith.sayHi() smith.sayBye() smith.sayAge(49) print '-'*30 # a spanish speaking person diaz = Person('Payne Diaz', 'spanish') diaz.sayHi() diaz.sayBye() diaz.sayAge(37) print '-'*30 # a german speaking person kiese = Person('Bonefazius Kiesewetter', 'german') kiese.sayHi() kiese.sayBye() kiese.sayAge(28)
Comments (Newest First)
vartotojas | Light Poster | Aug 9th, 2006
•
•
•
•
OUCH- all those If's!. a more perfect motivation for subclassing, I can't imagine. Bad design - hope this was very early on and you refactored it next?!
vegaseat | Kickbutt Moderator | Jun 10th, 2005
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
You could use a kludge like this:
print 'Un caluroso adi'+chr(243)+'s de', self.myName