Created
May 4, 2016 08:35
-
-
Save funkybob/6bf220b45b01e6a5751eeed4a932ba76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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