On Jan 5, 1:27 am, Volker Braun <vbraun.n...@gmail.com> wrote:
> Also, x.__abs__() is just the Python magic method for abs(x). The function
> call syntax abs(x) will always call __abs__ and return an AttributeError if
> it is not defined. So all we have is an x.abs() method that behaves exactly
> like abs(x), for better or worse.

Ah, thanks. THAT's the answer I was looking for. I didn't know python
had magic for abs. Indeed, '__abs__' is listed under the operators in
the python doc. We're not exactly succeeding in letting the method
behave exactly the same as the function, though. In plain python:

>>> class A(object):
...     def __abs__(self):
...         return "called"
...
>>> a=A()
>>> abs(a)
'called'
>>> class B(object): pass
...
>>> b=B()
>>> abs(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'B'

Possible alternatives:

 * delete 'RingElement.abs'. Then any subclass that wants absolute
values needs to implement __abs__ (that's necessary anyway) and, if it
wants the method accessible in another form as well, also a suitable
'abs'. I could see why that's not so desirable

 * change 'RingElement.abs' to be a bit more intelligent in looking up
the __abs__ method and throw a more informative error, such as
"TypeError: bad operand type" if it's not there.

 * implement "RingElement.abs(self)" as "return abs(self)" to fully
dispatch through python's standard channels.

We're just interfacing with Python's standard machinery here. No need
for extra subclasses/categories.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To post to this group, send email to sage-devel@googlegroups.com.
To unsubscribe from this group, send email to 
sage-devel+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.


Reply via email to