Skip to content

Instantly share code, notes, and snippets.

@funkybob
Created May 4, 2016 08:35
Show Gist options
  • Save funkybob/6bf220b45b01e6a5751eeed4a932ba76 to your computer and use it in GitHub Desktop.
Save funkybob/6bf220b45b01e6a5751eeed4a932ba76 to your computer and use it in GitHub Desktop.
setup = '''
class prop:
def __init__(self, getter):
self.getter = getter
def __get__(self, instance, owner=None):
if instance is None:
return self
value = instance.__dict__[self.getter.__name__] = self.getter(instance)
# Uncomment this to see that prop doesn't call more than once, but prap does.
# print(value)
return value
class prap(prop):
def __set__(self, instance, value):
raise AttributeError()
class X:
@prop
def val1(self):
return True
@prap
def val2(self):
return False
x = X()
'''
import timeit
print(timeit.timeit('x.val1', setup))
print(timeit.timeit('x.val2', setup))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment