User Name Password Register
DaniWeb IT Discussion Community
All
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:
Jun 10th, 2005
Views: 3,544
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.
python Syntax | 5 stars
  1. # multilingual phrase example to explain a Python class
  2. # classes of objects are defined with the keyword class
  3. # class names usually start with a capital letter
  4. # __init__() method is analogous to a constructor in C++, C# or Java
  5. # self identifies the class namespace
  6. # myName and self.myName are two different variables
  7. #
  8. # used AltaVista - Babel Fish Translation at
  9. # http://babelfish.altavista.com/
  10. #
  11. # ASCII characters above chr(127) are sometimes difficult to display
  12. # with some IDEs like DrPython and BOA, use PythonWin or IDLE
  13. #
  14. # Python24 tested vegaseat 10jun2005
  15.  
  16. class Person:
  17. def __init__(self, myName, language):
  18. self.myName = myName
  19. self.language = language
  20.  
  21. def sayHi(self):
  22. if self.language == 'german':
  23. print 'Hallo, mein Name ist', self.myName
  24. elif self.language == 'english':
  25. print 'Hello, my name is', self.myName
  26. elif self.language == 'spanish':
  27. print 'Hola, mi nombre es', self.myName
  28. else:
  29. print 'language name incorrect!'
  30.  
  31. def sayBye(self):
  32. if self.language == 'german':
  33. print 'Ein herzliches auf Wiedersehen von', self.myName
  34. elif self.language == 'english':
  35. print 'A hearty goodbye from', self.myName
  36. elif self.language == 'spanish':
  37. # DaniWeb codefield problem with chr(243) in "adiós"
  38. #print 'Un caluroso adios de', self.myName
  39. # could use a kludge like this ...
  40. print 'Un caluroso adi'+chr(243)+'s de', self.myName
  41. else:
  42. print 'language name incorrect!'
  43.  
  44. def sayAge(self, age):
  45. if self.language == 'german':
  46. print self.myName, "ist", age, "Jahre alt"
  47. elif self.language == 'english':
  48. print self.myName, "is", age, "years old"
  49. elif self.language == "spanish":
  50. # DaniWeb codefield problem with chr(241) in "años"
  51. #print self.myName, "es", age, "anos de viejo"
  52. print self.myName, "es", age, "a"+chr(241)+"os de viejo"
  53. else:
  54. print 'language name incorrect!'
  55.  
  56. print '-'*30 # 30 dashes, cosmetic stuff
  57. # you can use this
  58. Person('Ronald Smith', 'english').sayHi()
  59.  
  60. print '-'*30
  61. # or better use an alias to save typing and improve readability
  62. # (constructs an instance of Person Smith and creates a reference to it in smith)
  63. smith = Person('Ronald Smith', 'english')
  64. smith.sayHi()
  65. smith.sayBye()
  66. smith.sayAge(49)
  67.  
  68. print '-'*30
  69. # a spanish speaking person
  70. diaz = Person('Payne Diaz', 'spanish')
  71. diaz.sayHi()
  72. diaz.sayBye()
  73. diaz.sayAge(37)
  74.  
  75. print '-'*30
  76. # a german speaking person
  77. kiese = Person('Bonefazius Kiesewetter', 'german')
  78. kiese.sayHi()
  79. kiese.sayBye()
  80. 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
The DaniWeb codefield goofs up some of the special Spanish characters, sorry!
You could use a kludge like this:
print 'Un caluroso adi'+chr(243)+'s de', self.myName
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC