On Thu, Aug 19, 2010 at 03:59:14PM -0700, Simon King wrote: > Hi Jeroen, > > On 19 Aug., 23:54, Jeroen Demeyer <jdeme...@cage.ugent.be> wrote: > > I noticed some classes in Sage have a _pari_ method (which seems to be > > used to convert self to a PARI GEN). But there is also _pari_init_, > > which seems to be more or less the same (although, through a string). > > What is the point of this? If there is any documentation about this, > > feel free to point it out to me. > > I don't know where it is in the documentation. But the general > situation for all interfaces is, AFAIK, like this: > > Let "foo" be any interface such as singular, gap, pari, magma, .... > Let X be an object in Sage, and assume that you want to use foo on X. > Doing > sage: foo(X) > will result in trying to call a method X._foo_ with argument the > specified instance of the interface. If this is not implemented, the > method X._interface_ is called. You can see the source code of the > default implementation, e.g., by > sage: QQ._interface_?? > > In many cases, it is reasonable to cache the interface representation > of X. Hence, you would like that > sage: foo(X) is foo(X) > True > > The solution is, typically, that some representation of X is created > in foo if foo(X) is called for the first time. This representation is > known in foo under the name foo(X).name(). And if caching is done > (i.e., if X._interface_is_cached() returns True) then X._foo_(foo) is > supposed to always return the same instance that was created the first > time. > > But how is the representation created in the first place? That's the > job of the method X._foo_init_. It is supposed to return a string that > yields a representation of X when evaluated in foo. If X._foo_init_ is > not there, X._interface_init_ is called, which by default returns > repr(X) (which often makes sense, but of course not always). > > I hope that was clear enough. More or less, but not quite completely. What if X._foo_ already uses caching? Should I do something like:
class X: def foo_representation(self): # Existing function returning a representation of X in foo, with caching. # This function might exist because a lot of functionality of X is # implemented through interface foo. def _foo_(self): return self.foo_represenation() def _foo_init_(self): # Hope this is never called, because it is very inefficient! return str(self.foo_represenation()) -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org