This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: __float__ not called by 'float' on classes derived from str
Type: behavior Stage: needs patch
Components: Interpreter Core Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, r.david.murray, shura_zam
Priority: normal Keywords: patch

Created on 2009-04-15 03:41 by shura_zam, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue5759-trunk-test.patch r.david.murray, 2009-04-15 15:26
issue5759-py3k-test.patch r.david.murray, 2009-04-15 15:26
Messages (4)
msg85979 - (view) Author: Alexandr Zamaraev (shura_zam) Date: 2009-04-15 03:41
Test case:
[code]
class S:
    def __init__(self, v):
        self.data = v

    def __int__(self):
        print("S.INT called")
        return int(str(self.data))
    def __float__(self):
        print("S.FLOAT called")
        return float(str(self.data))
        

class T(str):
    def __int__(self):
        print("T.INT called")
        return int(str(self))
    def __float__(self):
        print("T.FLOAT called")
        return float(str(self))

class U(unicode):
    def __int__(self):
        print("U.INT called")
        return int(unicode(self))
    def __float__(self):
        print("U.FLOAT called")
        return float(unicode(self))


i = S("123")
print(type(int(i)))
print(type(float(i)))

i = T("123")
print(type(int(i)))
print(type(float(i))) # <<< CALLS __float__ NOTHING

i = U("123")
print(type(int(i)))
print(type(float(i)))
[/code]
Output:
[code]
S.INT called
<type 'int'>
S.FLOAT called
<type 'float'>
T.INT called
<type 'int'>
<type 'float'>
U.INT called
<type 'int'>
U.FLOAT called
<type 'float'>
[/code]
msg85980 - (view) Author: Alexandr Zamaraev (shura_zam) Date: 2009-04-15 03:42
read from http://community.livejournal.com/ru_python/232808.html
msg85993 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-04-15 15:26
I have confirmed this in trunk and py3k, unit tests attached.
msg86002 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-04-15 21:27
Thanks for the tests. Fixed in r71627.
History
Date User Action Args
2022-04-11 14:56:47adminsetgithub: 50009
2009-04-15 21:27:16benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg86002

resolution: fixed
2009-04-15 15:26:30r.david.murraysetfiles: + issue5759-py3k-test.patch
2009-04-15 15:26:12r.david.murraysetfiles: + issue5759-trunk-test.patch


keywords: + patch
stage: needs patch
title: Do not call __float__ to classes derived from str -> __float__ not called by 'float' on classes derived from str
nosy: + r.david.murray
versions: + Python 2.6, Python 3.0, Python 3.1, Python 2.7, - Python 2.5
messages: + msg85993
priority: normal
components: + Interpreter Core, - None
type: behavior
2009-04-15 03:42:05shura_zamsetmessages: + msg85980
title: Do not call __float__ to classec derived from str -> Do not call __float__ to classes derived from str
2009-04-15 03:41:01shura_zamcreate