Last active
July 3, 2019 18:04
-
-
Save pca2/05efd65df27245e24878a6f7b8610ae3 to your computer and use it in GitHub Desktop.
Basic Animal class example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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