Skip to content

Instantly share code, notes, and snippets.

@mw44118
Created December 10, 2013 02:23
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 mw44118/7884829 to your computer and use it in GitHub Desktop.
Save mw44118/7884829 to your computer and use it in GitHub Desktop.
Silly example of defining a class inside another class
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