Tuesday, December 18, 2018

Python Class

Python Class is easy
# Example class

class Animal():
    def whoami (self):
        print ("I am an animal.")

    def sound_like (self, string):
        print ("Animal sounds like: " + string)

    def get_limps (self, i):
        print ("Animal has " + i + " limps.")

a = Animal()
a.whoami()
 
Inheritance
# Example class inheritance

class Dog (Animal):
    def whoami (self):
        Animal.whoami (self)
        super().whoami()
        print ("I am a Dog!")

b = Dog()
b.whoami()
 

No comments:

Post a Comment