Experimenting with Python Class

vegaseat 1 Tallied Votes 202 Views Share

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)
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

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

vartotojas 0 Light Poster

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?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.