Created
May 26, 2012 15:30
-
-
Save wakhub/2794328 to your computer and use it in GitHub Desktop.
Liger and Tigon
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
import random | |
class Female(object): pass | |
class Male(object): pass | |
def get_sex(): | |
return [Female, Male][random.randint(0, 1)] | |
class Animal(object): | |
def __init__(self, name, sex=None): | |
self.name = name | |
self.sex = sex or get_sex() | |
def introduction(self): | |
print("I'm '%s' of %s." % (self.name, type(self).__name__)) | |
def can_crossing_to(self, target): | |
return self.sex != target.sex | |
def __add__(self, other): | |
if self.can_crossing_to(other) and type(other) == type(self): | |
return lambda name: type(self)(name) | |
raise Exception("Miss match") | |
class Lion(Animal): | |
def hunting(self): | |
print("Hunt for group") | |
def __add__(self, other): | |
if self.can_crossing_to(other) and type(other) == Tiger: | |
if self.sex == Male: | |
return lambda name: Liger(name) | |
else: | |
return lambda name: Tigon(name) | |
return Animal.__add__(self, other) | |
class Tiger(Animal): | |
def hunting(self): | |
print("Hunt for me") | |
def __add__(self, other): | |
if self.can_crossing_to(other) and type(other) == Lion: | |
if self.sex == Male: | |
return lambda name: Tigon(name) | |
else: | |
return lambda name: Ligon(name) | |
return lambda name: Liger(name) | |
return Animal.__add__(self, other) | |
class Liger(Lion, Tiger): | |
def hunting(self): | |
print("Hunt for ...!") | |
class Tigon(Tiger, Lion): | |
def hunting(self): | |
print("Hunt for ...?") | |
def main(): | |
lion1 = Lion(name="Lion hart") | |
tiger1 = Tiger(name="Tiger uppercut", sex=Male if lion1.sex == Female else Female) | |
hybrid = (lion1 + tiger1)(name="Hybrid super animal") | |
lion1.introduction() | |
lion1.hunting() | |
tiger1.introduction() | |
tiger1.hunting() | |
hybrid.introduction() | |
hybrid.hunting() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment