Skip to content

Instantly share code, notes, and snippets.

Last active April 19, 2022 21:35
What would you like to do?
Python closures vs methods
from timeit import timeit
import inspect
def t():
for f in inspect.stack():
s = """
class Object:
def __init__(self):
def A(x):
self = Object()
def bar(y):
return x+y = bar
return self
class B:
def __init__(self, x):
self.x = x
def bar(self, y):
return self.x + y
a = A(3)
b = B(3)
a = """
a = A(3)
b = """
b = B(3)
c = """"""
d = """"""
print(timeit(a, s, number=1000000))
print(timeit(b, s, number=1000000))
print(timeit(c, s, number=1000000))
print(timeit(d, s, number=1000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment