Hi Gennady,

On 2014-05-11, Gennady Uraltsev <[email protected]> wrote:
> Following the tutorial I should first start by defining the new elements. 
> In the tutorial the elements inherit from the generic "FieldElement". I 
> would like to inherit from "Integer"
> sage: class NewIntEl(Integer):
>         def __init__(self, parent, x=None):
>                 Integer.__init__(self, x)
>         def _repr_(self):
>                 return "NewInt representation funtion says: %s"%Integer.

This should be it---but unfortunately the Integer class is not
implemented in the way recommended in the tutorials: It does not use the
default __repr__ method (double underscore), but overrides it.

So, if one sub-classes Integer, then one has to override __repr__, since
the single underscore _repr_ (which one should usually implement) is
ignored.

However, things are worse: By some trickery, the creation of instances
of Integer is overridden by something that is very fast. The price to
pay is that it is very difficult to subclass the Integer class. In your
example, you will actually find that NewIntEl(ZZ,1) is of type
sage.rings.integer.Integer, but *not* of type NewIntEl.

But not all is lost. You may have a look at the source code of
sage.rings.integer, where you find the "trickery" (search for "pool", I
think it is a very interesting trick to save time for element creation),
and you find the class IntegerWrapper.

This can be used:

  sage: from sage.rings.integer import IntegerWrapper
  sage: class MyInteger(IntegerWrapper):
  ....:     def __repr__(self):
  ....:         return "MyInteger(%s)"%self.str()
  ....:     
  sage: a = MyInteger(ZZ, 1)
  sage: a
  MyInteger(1)
  sage: isinstance(a, Integer)
  True
  sage: type(a)
  <class '__main__.MyInteger'>

> Obviously continuing to write down the definition for the new class of 
> NewInt doens't make sense because this part doesn't work.

Yes, that won't make sense. But I think IntegerWrapper could be a
starting point.

> I cannot find any 
> clue as what is wrong. From other experiments it seems that the __init__ 
> doesn't get executed AT ALL.

That may be part of the trickery...

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" 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-support.
For more options, visit https://groups.google.com/d/optout.

Reply via email to