Last active
March 27, 2019 20:40
-
-
Save ozcanyarimdunya/a1483cc325e3e32ec638fb055772a499 to your computer and use it in GitHub Desktop.
How to write class based generic python
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
""" | |
Animal | |
======= | |
+ name | |
+ legs | |
""" | |
class Animal(object): | |
name = None | |
legs = None | |
def get_name(self): | |
assert self.name is not None, ( | |
"Any {} should have `name` attribute".format(self.__class__.__name__) | |
) | |
return self.name | |
def get_legs(self): | |
assert self.legs is not None, ( | |
"Any {} should have `leg` attribute".format(self.__class__.__name__) | |
) | |
assert self.legs > 0, ( | |
"Any {} should have at least 1 `leg`".format(self.__class__.__name__) | |
) | |
return self.legs | |
def write(self): | |
return "{} {}".format(self.get_name(), self.get_legs()) | |
class Mixin(object): | |
def prettify(self): | |
return "{} has {} legs".format(self.get_name(), self.get_legs()) | |
class Bird(Mixin, Animal): | |
name = 'bird' | |
legs = 1 | |
bird = Bird() | |
bird.write() | |
bird.prettify() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment