"""deferred_binder module"""
class DeferredBinder:
"""A descriptor that defers binding for an object.
The binding is delayed until the name to which the descriptor is
bound is accessed. This is also the name to which the object would
have been bound if the descriptor hadn't been used.
"""
def __init__(self, name, target):
self.name = name
self.target = target
def __get__(self, obj, cls):
context = (cls.__dict__, cls)
target = self.transform(self.name, context, self.target, obj)
setattr(cls, self.name, self.target)
return target
@staticmethod
def transform(name, context, target, obj=None):
"""Transform the target and return it.
name - the name to which the target will be bound.
context - namespace, and optionally the class, in which the
target will be bound to the name.
obj - the instance of the class that is involved in calling
this method, if any.
"""
return target
@staticmethod
def is_deferred(f):
"""A decorator."""
return DeferredBinder(f.__name__, f)
Diff to Previous Revision
--- revision 3 2011-06-11 00:33:48
+++ revision 4 2011-08-12 23:42:27
@@ -1,7 +1,7 @@
"""deferred_binder module"""
class DeferredBinder:
- """A descriptor that defers binding for one object.
+ """A descriptor that defers binding for an object.
The binding is delayed until the name to which the descriptor is
bound is accessed. This is also the name to which the object would
@@ -14,8 +14,9 @@
self.target = target
def __get__(self, obj, cls):
- target = self.transform(obj, cls, name, target)
- setattr(cls, self.name, target)
+ context = (cls.__dict__, cls)
+ target = self.transform(self.name, context, self.target, obj)
+ setattr(cls, self.name, self.target)
return target
@staticmethod