#12601: @cached_method does not work for special methods
-------------------------------------------------+-------------------------
       Reporter:  saraedum                       |        Owner:  jason
           Type:  enhancement                    |       Status:  new
       Priority:  minor                          |    Milestone:  sage-5.12
      Component:  misc                           |   Resolution:
       Keywords:  cached_method, cache,          |    Merged in:
  operator, special method                       |    Reviewers:
        Authors:                                 |  Work issues:
Report Upstream:  Reported upstream. No          |       Commit:
  feedback yet.                                  |     Stopgaps:
         Branch:                                 |
   Dependencies:                                 |
-------------------------------------------------+-------------------------

Comment (by SimonKing):

 I think I found the problem. Since the special method of the class is
 called, the `__get__` method of `CachedMethod` is relevant. Its purpose is
 to return a "cached method caller" that is bound to the instance.

 This `__get__` method currently supposes that it is ''only'' called if the
 attribute is not overridden on the instance's `__dict__`. Namely, if the
 `CachedMethodCaller` is in the instance's `__dict__`, then Python's
 attribute lookup will not care about the `CachedMethod` if the instance's
 class.

 Hence, `__get__` currently does
 {{{
 #!python
         # This is for Parents or Elements that do not allow attribute
 assignment:
         try:
             name = self._cachedfunc.__name__
         except AttributeError:
             name = self.__name__
         try:
             return (<dict>inst.__cached_methods).__getitem__(name)
         except (AttributeError,TypeError,KeyError),msg:
             pass
 }}}
 and would then create a ''new'' `CachedMethodCaller`. However, in the
 example from the ticket description, there already is a
 `CachedMethodCaller` in the instance's `__dict__`, but it is ignored by
 the `__get__` method of the `CachedMethod`.

 So, a potential solution is to additionally check
 `inst.__dict__.__getitem__(name)`.

 What I don't like about this solution is that it would involve an
 additional dictionary lookup, which would slow-down every call to a cached
 method, fortunately only in the case that the instance does not support
 attribute assignment. And in addition, the call to the special method will
 ''always'' involve running this `__get__` method and doing a dictionary
 lookup.

 Alternatively, one could create a special kind of cached method,
 specifically for special methods of new style classes whose instances
 allow for attribute assignment.

--
Ticket URL: <http://trac.sagemath.org/ticket/12601#comment:8>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to