Skip to content

Instantly share code, notes, and snippets.

@ozcanyarimdunya
Last active March 27, 2019 20:40
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 ozcanyarimdunya/a1483cc325e3e32ec638fb055772a499 to your computer and use it in GitHub Desktop.
Save ozcanyarimdunya/a1483cc325e3e32ec638fb055772a499 to your computer and use it in GitHub Desktop.
How to write class based generic python
"""
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