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

noreply@sourceforge.net noreply@sourceforge.net
Fri, 17 Aug 2001 10:21:22 -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: None
>Status: Closed
>Resolution: Rejected
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: Guido van Rossum (gvanrossum)
Date: 2001-08-17 10:21

Message:
Logged In: YES 
user_id=6380

Ah, I misread.  __float__ won't work.

But still, round() is the wrong operator to use: while it
indeed returns a rounded value, it does not return an 'int'
-- it returns a float with an integral value (subtle
difference).

I would suggest that rather than abusing the built-in
functions int() etc., your class should define its own
non-special methods to return values other than real Python
ints. The int() function really should return an int at all
times. You are violating this, so apparently the caller
knows that the argument is an instance of your class -- so
they should be able to know to call a method that your class
defines.

I'm closing the request.

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

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