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

noreply@sourceforge.net noreply@sourceforge.net
Fri, 17 Aug 2001 09:55:05 -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: 5
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 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