29.05.20 12:48, Mark Dickinson пише:
2. Call the object's `__float__` method. But this is fraught with peril, too: 
for a proper equivalent, you need to be careful to look up `__float__` on the 
type, not the object itself. And then a new version of Python changes 
`PyFloat_AsDouble` to also accept objects with `__index__`, and suddenly your 
version no longer matches what Python does. (This happened.)

There is yet one problem with calling the object's `__float__` method. It looks up `__float__` as object's attribute instead of type's attribute, so it is possible to override it for individual object.

So my modest proposal is: expose the conversion represented by `PyFloat_AsDouble` to 
Python somewhere: either on the `float` type itself, or somewhere in the standard 
library. My question for everyone on this list is: _if_ it were to be added, where should 
it go? (There's also the "what should it be called" question, of course.)

I prefer it to be an alternative float constructor. We can also add the corresponding constructor for complex, and add constructors which accept only str, bytes or bytes-like object (i.e. parse a text representation of the number). See also previous discussion about alternative constructors. [1]

[1] https://mail.python.org/archives/list/python-ideas@python.org/thread/5JKQMIC6EUVCD7IBWMRHY7DRTTNSBOWG/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EVBKNRLFHCBHVPRN74WUSXO4YEXCKUCD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to