On Oct 20, 2009, at 4:39 AM, Florent Hivert wrote: > > Hi there, > > I'm writing a parent whose elements contain an attribute called > value which is > an Integer. I'd like to have a coercion to the integer. So I wrote the > following in the __init__ method of my parent > > mor = Hom(self, IntegerRing())(lambda z: z.value) > mor.register_as_coercion() > self._populate_coercion_lists_() > > Now the following works: > sage: NN = NonNegativeIntegers_Alternative() > sage: x = NN.an_element() > sage: ZZ(x) > 42 > > But > sage: Integer(x) > Traceback (most recent call last): > ... > TypeError: unable to coerce <class > 'sage > .categories > .examples > .infinite_enumerated_sets > .NonNegativeIntegers_Alternative_with_category.element_class'> to an > integer > > Is it possible to declare a coercion to a type (Integer) as opposite > to a > parent ? Am I doing something wrong ? Thanks for your help.
There isn't without modifying the Integer's __init__ method itself. However, for integers you can implement the _integer_ method (which won't provide coercion, but will provide conversion). - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send an email to [email protected] To unsubscribe from this group, send an email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
