Sage: Ticket #29037: cached_method does not work when the result of the method is None
https://trac.sagemath.org/ticket/29037
<pre class="wiki">sage: class Foo(object):
....: def __init__(self):
....: self.x = None
....: @cached_method
....: def bar(self):
....: if self.x is None:
....: return None
....: return 5
....:
sage: f = Foo()
sage: f.bar()
sage: f.x = -2
sage: f.bar()
5
sage: class Foo2(object):
....: def __init__(self):
....: self.x = None
....: @cached_method
....: def bar(self):
....: if self.x is None:
....: return -1
....: return 5
....:
sage: f2 = Foo2()
sage: f2.bar()
-1
sage: f2.x = 0
sage: f2.bar()
-1
</pre><p>
Sometimes it is useful to do a long computation and return <code>None</code> (say, because you realize something is impossible) and then store that computation. Furthermore, this is not a documented special case AFAIK, so even if we do not change the behavior, we need to add it to the documentation.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/29037
Trac 1.1.6SimonKingSat, 18 Jan 2020 15:12:25 GMT
https://trac.sagemath.org/ticket/29037#comment:1
https://trac.sagemath.org/ticket/29037#comment:1
<p>
Replying to <a class="new ticket" href="https://trac.sagemath.org/ticket/29037" title="defect: cached_method does not work when the result of the method is None (new)">tscrim</a>:
</p>
<blockquote class="citation">
<p>
Sometimes it is useful to do a long computation and return <code>None</code> (say, because you realize something is impossible) and then store that computation. Furthermore, this is not a documented special case AFAIK...
</p>
</blockquote>
<p>
Yes it is documented:
</p>
<div class="wiki-code"><div class="code"><pre>cdef <span class="k">class</span> <span class="nc">CachedMethodCallerNoArgs</span><span class="p">(</span>CachedFunction<span class="p">):</span>
<span class="s">"""
Utility class that is used by :class:`CachedMethod` to bind a
cached method to an instance, in the case of a method that does
not accept any arguments except ``self``.
.. NOTE::
The return value ``None`` would not be cached. So, if you have
a method that does not accept arguments and may return ``None``
after a lengthy computation, then ``@cached_method`` should not
be used.
...
</span></pre></div></div>
TickettscrimSat, 18 Jan 2020 15:15:00 GMT
https://trac.sagemath.org/ticket/29037#comment:2
https://trac.sagemath.org/ticket/29037#comment:2
<p>
I see; I didn't think of looking there. Perhaps we can make that more prominent, perhaps in the <code>cached_method</code> decorator?
</p>
TicketmkoeppeTue, 14 Apr 2020 19:41:51 GMTmilestone changed
https://trac.sagemath.org/ticket/29037#comment:3
https://trac.sagemath.org/ticket/29037#comment:3
<ul>
<li><strong>milestone</strong>
changed from <em>sage-9.1</em> to <em>sage-9.2</em>
</li>
</ul>
<p>
Batch modifying tickets that will likely not be ready for 9.1, based on a review of the ticket title, branch/review status, and last modification date.
</p>
TicketmkoeppeSat, 29 Aug 2020 16:00:51 GMTcc changed
https://trac.sagemath.org/ticket/29037#comment:4
https://trac.sagemath.org/ticket/29037#comment:4
<ul>
<li><strong>cc</strong>
<em>klee</em> added
</li>
</ul>
TicketmkoeppeSat, 29 Aug 2020 16:37:33 GMTmilestone changed
https://trac.sagemath.org/ticket/29037#comment:5
https://trac.sagemath.org/ticket/29037#comment:5
<ul>
<li><strong>milestone</strong>
changed from <em>sage-9.2</em> to <em>sage-9.3</em>
</li>
</ul>
TicketmkoeppeSat, 13 Feb 2021 20:51:01 GMTmilestone changed
https://trac.sagemath.org/ticket/29037#comment:6
https://trac.sagemath.org/ticket/29037#comment:6
<ul>
<li><strong>milestone</strong>
changed from <em>sage-9.3</em> to <em>sage-9.4</em>
</li>
</ul>
<p>
Setting new milestone based on a cursory review of ticket status, priority, and last modification date.
</p>
TicketmkoeppeMon, 19 Jul 2021 01:43:17 GMTmilestone changed
https://trac.sagemath.org/ticket/29037#comment:7
https://trac.sagemath.org/ticket/29037#comment:7
<ul>
<li><strong>milestone</strong>
changed from <em>sage-9.4</em> to <em>sage-9.5</em>
</li>
</ul>
Ticket