Skip to content

Instantly share code, notes, and snippets.

@grzhan
Created October 18, 2014 15:34
Show Gist options
  • Save grzhan/89c196b45bd8da24a4cd to your computer and use it in GitHub Desktop.
Save grzhan/89c196b45bd8da24a4cd to your computer and use it in GitHub Desktop.
import copy
import functools
class B(object):
def __init__(self, name):
self.name = name
class A(object):
def __init__(self):
self.apps = {}
def create_app(self, name):
app = B(name)
if name not in self.apps:
self.apps[name] = app
return app
a = A()
b1 = a.create_app("111")
b2 = a.create_app("222")
b3 = a.create_app("333")
for name, app in a.apps.items():
app.get_name = lambda: name
for name, app in a.apps.items():
def namegetter():
new_name = copy.deepcopy(name)
return new_name
app.get_name2 = namegetter
print(b1.get_name())
print(b2.get_name())
print(b3.get_name())
print(b1.get_name2())
print(b2.get_name2())
print(b3.get_name2())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment