Last active
November 1, 2016 22:29
-
-
Save kn9ts/2e02cade25dc92f24c9760d2d1e9194d to your computer and use it in GitHub Desktop.
An example of getting an attribute from a child class
This file contains hidden or 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 Car(object): | |
def __init__(self, model, engine, fuel_capacity, tires): | |
self.model = model | |
self.tires = tires | |
self.engine = engine | |
self.fuel_capacity = fuel_capacity | |
def drive(self, text): | |
print text + ' ' + self.model | |
class Engine(object): | |
def __init__(self, size): | |
self.size = size | |
class Tires(object): | |
def __init__(self, model, number, has_spare_tire=False): | |
self.model = model | |
self.number_of_tires = number | |
self.has_spare_tire = has_spare_tire | |
class Toyota(Car): | |
def __init__(self, ): | |
self.model = 'Corolla' | |
self.engine = 'v4' | |
self.fuel_capacity = '1500 cc' | |
self.tires = 4 | |
super(Toyota, self).drive('I am driving a') | |
def print_tires(self): | |
print self.tires.model | |
ToyotaCarInstance = Car.__subclasses__()[0]() | |
print ToyotaCarInstance | |
print ToyotaCarInstance.model | |
print ToyotaCarInstance.fuel_capacity |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment