[Python-bugs-list] [ python-Bugs-450543 ] round() needs a special method name

noreply@sourceforge.net noreply@sourceforge.net
Fri, 17 Aug 2001 10:13:42 -0700


Bugs item #450543, was opened at 2001-08-13 10:27
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=450543&group_id=5470

Category: Python Interpreter Core
Group: Python 2.1.1
Status: Open
Resolution: None
>Priority: 1
Submitted By: Tessa Lau (tlau)
Assigned to: Nobody/Anonymous (nobody)
Summary: round() needs a special method name

Initial Comment:
Special methods may be defined to override the builtin
functions int(), float(), etc.  However I see no way to
override the builtin function round().

I am defining a class library for manipulation of
int/float/char/etc. sequences.  My class FloatSequence
defines the method __int__ which returns an instance of
IntSequence, and so on.  For example:
int(FloatSequence(1.0, 2.4)) returns IntSequence(1,2).

However I cannot define a method __round__ in my
FloatSequence class to return an IntSequence.  Calling
round(FloatSequence(1.0, 2.3)) results in the error:

AttributeError: FloatSequence instance has no attribute
'__float__'

----------------------------------------------------------------------

>Comment By: Tessa Lau (tlau)
Date: 2001-08-17 10:13

Message:
Logged In: YES 
user_id=112896

I agree that it's pretty esoteric, and if adding a special
method is a lot of work, then I retract this bug report. 
Conceptually, I thought of round() as a unary operator, and
so it would have been nice to have it be overridable like
the other operators.  I also don't see how float() could be
used instead of round(); I want to convert a float to an int
by rounding off the decimal portion.  Why does round() call
the __float__ special method?

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-17 09:55

Message:
Logged In: YES 
user_id=6380

Would calling float() instead of round() work?  That's what
the __float__ special method is for.  Adding a new special
method is  a large amount of work, and this seems a pretty
esoteric situation, so I'm not sure it's a good idea to
accept this request.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=450543&group_id=5470