Skip to content

Instantly share code, notes, and snippets.

@dfbarrero
Last active March 13, 2020 10:53
Show Gist options
  • Select an option

  • Save dfbarrero/cd2e32f810dbd6f3f62ac7f33e2c67da to your computer and use it in GitHub Desktop.

Select an option

Save dfbarrero/cd2e32f810dbd6f3f62ac7f33e2c67da to your computer and use it in GitHub Desktop.
Example of inheritance.
class Animal:
def __init__(self):
self.name = "Unknown"
self.age = 10
def describe(self):
print("Name: ", self.name)
print("Age: ", self.age)
class Dog(Animal):
def bit(self):
print(self.name + " has bitten")
class Cat(Animal):
def scratch(self):
print(self.name + " has scratched")
if __name__ == '__main__':
snoopy = Dog()
garfield = Cat()
snoopy.name = "Snoopy"
garfield.name = "Garfield"
snoopy.bit()
garfield.scratch()
garfield.bit() # Error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment