Skip to content

Instantly share code, notes, and snippets.

@pca2
Last active July 3, 2019 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pca2/05efd65df27245e24878a6f7b8610ae3 to your computer and use it in GitHub Desktop.
Save pca2/05efd65df27245e24878a6f7b8610ae3 to your computer and use it in GitHub Desktop.
Basic Animal class example
class Animal:
class_var = 'foo'
def __init__(self, name, age=11):
self.name = name
self.age = age
def greet(self):
print(f"Hello, I'm {self.name}")
class Dog(Animal):
def bark(self):
print("Woof")
animal = Animal('animal')
animal.greet()
"Hello I'm animal"
print(animal.name)
"animal"
Fido = Dog('Fido',16)
Fido.bark()
'Woof'
Fido.greet()
"Hello, I'm Fido"
print(Fido.class_var)
'foo'
class Poodle(Dog):
pass #needed when you don't want to add anything, but the whitespace-based syntax demands some kind of line, like here
Class Cat(Animal):
#overriding the Parent greet by simply redefining it in the child class
def greet(self):
print('meow')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment