Created
July 13, 2009 08:13
-
-
Save Artanis/145993 to your computer and use it in GitHub Desktop.
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 Base(object): | |
def __init__(self): | |
pass | |
def combine(self, other): | |
""" Performs class husbandry | |
Returns: A tuple containing an instance of and reference to a | |
new class created with the two supplied classes as parents. | |
""" | |
# Get the list of base classes | |
# __class__ accesses the class the object references | |
# From there, __name__ gets the class name. | |
# So here we construct a tuple of the new parent classes | |
bases = (self.__class__, other.__class__) | |
# Gotta be called something. | |
# Autonaming convention: Concat the parent class names | |
name = "" | |
for base in bases: | |
name += base.__name__ | |
# When type() is called with three arguments it creates a new type | |
# from the given data. | |
dynamic = type(name, bases, {}) | |
return (dynamic(), dynamic) | |
class A(Base): pass | |
class B(Base): pass | |
a = A() | |
b = B() | |
c, AB = a.combine(b) | |
print c.__class__.__bases__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment