Skip to content

Instantly share code, notes, and snippets.

@ericls
Created February 27, 2021 15:56
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 ericls/3a2a17c7dbceb15462d0fcbe259d2246 to your computer and use it in GitHub Desktop.
Save ericls/3a2a17c7dbceb15462d0fcbe259d2246 to your computer and use it in GitHub Desktop.
make_c
class C:
def __init__(self):
print("here")
c_cache = {}
def make_c():
import inspect
frame = inspect.stack()
key = '.'.join([frame[1].filename, frame[1].function])
if key not in c_cache:
c_cache[key] = C()
return c_cache[key]
def foo():
c = make_c()
def bar():
c = make_c()
for i in range(100):
foo()
for i in range(100):
bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment