Tests for conditions in descriptors.

This commit is contained in:
Dave Halter
2014-08-13 14:49:42 +02:00
parent ec7b3bf433
commit f743619fb8
2 changed files with 38 additions and 1 deletions

View File

@@ -169,7 +169,7 @@ class Instance(use_metaclass(CachedMetaClass, Executed)):
""" Throws a KeyError if there's no method. """ """ Throws a KeyError if there's no method. """
# Arguments in __get__ descriptors are obj, class. # Arguments in __get__ descriptors are obj, class.
# `method` is the new parent of the array, don't know if that's good. # `method` is the new parent of the array, don't know if that's good.
args = [obj, obj.base] if isinstance(obj, Instance) else [None, obj] args = [obj, obj.base] if isinstance(obj, Instance) else [compiled.none_obj, obj]
return self.execute_subscope_by_name('__get__', args) return self.execute_subscope_by_name('__get__', args)
def scope_names_generator(self, position=None): def scope_names_generator(self, position=None):

View File

@@ -180,3 +180,40 @@ E.t(1)
e.u(1) e.u(1)
#? str() #? str()
E.u(1) E.u(1)
# -----------------
# Conditions
# -----------------
from functools import partial
class Memoize():
def __init__(self, func):
self.func = func
def __get__(self, obj, objtype):
if obj is None:
return self.func
return partial(self, obj)
def __call__(self, *args, **kwargs):
# We don't do caching here, but that's what would normally happen.
return self.func(*args, **kwargs)
class MemoizeTest():
def __init__(self, x):
self.x = x
@Memoize
def some_func(self):
return self.x
#? int()
MemoizeTest(10).some_func()
# Now also call the same function over the class (see if clause above).
#? float()
MemoizeTest.some_func(MemoizeTest(10.0))