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.
Jeroen.
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())
But this does not explain why _foo_init_ should return a STRING
(clearly, this is often a bad idea).
Jeroen.
Here's what is going on. We have the pexpect interface to GP (which
we will refer to as gp) as well as C interface to PARI (which we will
refer to as pari).
gp:
- Defined in sage/intefaces/gp.py
- Specifies name="pari" in the constructor
(sage/interfaces/gp.py:156), which would normally make gp(foo) try to
call foo._pari_(), but there is special case code in Expect.__call__
to change this to use foo._gp_() instead.
(sage/interfaces/expect.py:1056)
- foo._gp_(gp) which is supposed to return a GpElement
- SageObject provides a default implementation of _gp_() which
calls SageObject._interface_(gp), which in turn tries to call
_pari_init_ (since gp has name="pari")
- Generally, the thing returned from the _XXX_init_ methods is a
string, but what really matters is that it is some object such that
when it's passed to the __call__ method of the interface object, it
"returns the right thing". See sage/structure/sage_object.pyx:387.
- SageObject also defines a default implementation of _gp_init_
which just calls _pari_init_. I don't think this function is run
anywhere.
pari:
- Defined in sage/libs/pari/gen.pyx
- Uses foo._pari_() which is supposed to return a PARI GEN object.
sage/libs/pari/gen.pyx:8414
- SageObject defines a default implementation of _pari_() which will
try calling pari(foo._pari_init_()).
- pari("string") will eventually call gp_read_str("string") which
should return a GEN object.
The idea behind all of the _XXX_ and _XXX_init_ methods is that _XXX_
returns the actual object whereas _XXX_init_ returns something which
is fed into the parent's __call__ method.
The reason why the PARI situation is a bit more complicated is that
anything string you return from _gp_init_ should be valid as a
_pari_init_ function. We should really name the name="pari" in the gp
Expect object so that we can remove the special case code. We should
then also just have the default implementation of _gp_init_ call
_pari_init_ so that if you just define that, it will work for both gp
and pari.
Sorry it took so long to answer this.
--Mike