Anonymous committed 4dedc70

Try to save memory and time when using lazy objects by refering to their function via the closure, and not making it an attribute on each instance.

Comments (0)

Files changed (1)


         __dispatch = None
         def __init__(self, args, kw):
-            self.__func = func
             self.__args = args
             self.__kw = kw
             if self.__dispatch is None:
         def __reduce__(self):
             return (
-                (self.__func, self.__args, self.__kw) + resultclasses
+                (func, self.__args, self.__kw) + resultclasses
         def __prepare_class__(cls):
                 cls.__str__ = cls.__str_cast
         __prepare_class__ = classmethod(__prepare_class__)
-        def __promise__(cls, klass, funcname, func):
+        def __promise__(cls, klass, funcname, method):
             # Builds a wrapper around some magic method and registers that magic
             # method for the given type and method name.
             def __wrapper__(self, *args, **kw):
                 # Automatically triggers the evaluation of a lazy value and
                 # applies the given magic method of the result type.
-                res = self.__func(*self.__args, **self.__kw)
+                res = func(*self.__args, **self.__kw)
                 for t in type(res).mro():
                     if t in self.__dispatch:
                         return self.__dispatch[t][funcname](res, *args, **kw)
             if klass not in cls.__dispatch:
                 cls.__dispatch[klass] = {}
-            cls.__dispatch[klass][funcname] = func
+            cls.__dispatch[klass][funcname] = method
             return __wrapper__
         __promise__ = classmethod(__promise__)
         def __unicode_cast(self):
-            return self.__func(*self.__args, **self.__kw)
+            return func(*self.__args, **self.__kw)
         def __str_cast(self):
-            return str(self.__func(*self.__args, **self.__kw))
+            return str(func(*self.__args, **self.__kw))
         def __cmp__(self, rhs):
             if self._delegate_str:
-                s = str(self.__func(*self.__args, **self.__kw))
+                s = str(func(*self.__args, **self.__kw))
             elif self._delegate_unicode:
-                s = unicode(self.__func(*self.__args, **self.__kw))
+                s = unicode(func(*self.__args, **self.__kw))
-                s = self.__func(*self.__args, **self.__kw)
+                s = func(*self.__args, **self.__kw)
             if isinstance(rhs, Promise):
                 return -cmp(rhs, s)