Created
December 10, 2013 02:23
-
-
Save mw44118/7884829 to your computer and use it in GitHub Desktop.
Silly example of defining a class inside another 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 A(object): | |
def __init__(self, f, mood): | |
self.f = f | |
self.mood = mood | |
def make_class(self): | |
class C(object): | |
# self here refers to the instance of A, not an instance of | |
# C! | |
my_method = self.f | |
if self.mood == 'happy': | |
# But this "self" refers to the instance of C... | |
def walk(self): | |
print("With a spring in my step") | |
else: | |
def walk(self): | |
print("So glum") | |
return C | |
if __name__ == "__main__": | |
a = A(lambda self, x: x+x, 'happy') | |
bigC = a.make_class() | |
c = bigC() | |
print("c.my_method(4) returned {0}.".format(c.my_method(4))) | |
c.walk() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment