Hi Nils,

On 2013-02-02, Nils Bruin <nbr...@sfu.ca> wrote:
> Actually, we are completely misdiagnosing the problem. Indeed, your
> example works, but your function takes an argument on top of self. In
> that case all the other solutions work as expected as well. Florent is
> asking about a method without arguments (besides self). That case is
> special cased and none of the examples work.

Yes, you are right! Even worse, calling the alias makes the cache of the
original cached method go away:

  sage: class Foo(object):
  ....:     @cached_method
  ....:     def truc(self):
  ....:         print "calling truc"
  ....:         return 1
  ....:     
  sage: Foo.trac = Foo.truc
  sage: f = Foo()
  sage: f.trac is f.truc
  True
  sage: f.truc()
  calling truc
  1
  sage: f.truc()
  1
  sage: f.trac()
  calling truc
  1
  sage: f.truc()
  calling truc
  1
  sage: f.truc()
  1


> My solution doesn't
> either. That's probably because in that case the caching isn't done in
> the dictionary but on the function entry itself, for optimization
> purposes.

OK, but I still don't see why this would be a problem. After all, f.truc
*is* f.trac.

> That fundamentally doesn't work with aliases, ...

Why? I mean, apparently it doesn't work, but can you explain why f.truc
is f.trac, but f.trac can not find the cache (which is an attribute of
itself!), while f.truc *can* find it?

The cached method's __call__ method simply is
        if self.cache is None:
            f = self.f
            self.cache = f(self._instance)
        return self.cache
where "self" is the cached method (f.trac or f.truc in our example),
and self._instance is the instance on which it is bound (f in our
example).

Aha, now that's interesting:
  sage: f.truc()
  calling truc
  1
  sage: f.truc.cache
  1
  sage: f.trac.cache
  sage: f.truc.cache
  sage: 

In other words, even though f.trac is f.truc, accessing f.trac.cache
empties the previously existing f.truc.cache.

How can this be explained?

Cheers,
Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to