Hi Eric,

On 2015-11-23, Eric Gourgoulhon <egourgoul...@gmail.com> wrote:
> Le lundi 23 novembre 2015 10:55:40 UTC+1, Jeroen Demeyer a =C3=A9crit :
>>
>>  Of course, you could argue that Element=20
>> should not have __add__ and __mul__ (since not all elements can be added=
>=20
>> or multiplied). But it doesn't really hurt to have them and raise=20
>> NotImplementedError (unless I'm missing something).=20
>>
>>
> Maybe a too naive remark: what you propose seems against standard=20
> object-oriented programming. Isn't it more clear to have a generic base=20
> class Element and implement __add__ and __mul__ only in derived classes,=20
> when relevant? Sorry, I don't know the details of the element classes, so=
> you may have good reasons to do this...

Yes and no.

Yes, you want that addition only works for elements that can be added.

No, because EVERY element (not only those that can be added!) should be
aware of the coercion model.

That's to say, IMHO, there should be __add__ and __mul__ for Element, in
order to bind all elements to the coercion model. And if you want to
implement addition, then you can provide an "_add_" (single, not
double underscore!) method. Or you can define an additive action on the
level of the parent. Likewise for multiplication: Implement _mul_ for
multiplication (say, of two elements that belong to the same ring),
implement _rmul_ for the multiplicative action of the base ring on an algebra,
and implement further multiplicative actions if you need them.

That's perfectly OO!

Indeed, if you do not implement any of the above, then the default
__add__ method will fail with a TypeError. And if you implement any of
the above, then it will propagate to sub-types.

Best regards,
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to