[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

I think an alternative constructor is the best option. Some time ago I proposed 
to add more alternative constructors: 
https://mail.python.org/archives/list/python-id...@python.org/thread/5JKQMIC6EUVCD7IBWMRHY7DRTTNSBOWG/

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Mark Dickinson


Mark Dickinson  added the comment:

On naming, maybe float.from_number or float.from_numeric?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Mark Dickinson


Mark Dickinson  added the comment:

> `operator` seems a slightly odd place for this.

Yes, it's not ideal. It's by analogy with operator.index, which provides the 
equivalent duck-typing for integers, and calls __index__.  Similarly, 
operator.as_float is primarily there to call __float__, except that now that 
PyFloat_AsDouble also makes use of __index__, it'll call that, too.

My other thought was putting this in math, since it's what the math module is 
already doing implicitly to most inputs.

An alternative float constructor could work. Though we then run into the messy 
question of what it should do for float subclasses ...

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Zachary Ware


Zachary Ware  added the comment:

`operator` seems a slightly odd place for this.  My naive expectation would be 
that `float(floatlike_obj)` should do what you want, but it seems that's not 
the case (too permissive of input types?).  So then, what about an alternate 
constructor on the float object, `float.from_floatlike(obj)`?  This could be 
implemented as effectively:

class float:
@classmethod
def from_floatlike(cls, obj):
return cls(PyFloat_FromDouble(PyFloat_AsDouble(obj)))

which would work to get an instance of any float subclass after a round-trip 
through a double.  I have no idea whether that's actually useful, though :)

--
nosy: +zach.ware

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Mark Dickinson


Mark Dickinson  added the comment:

Proof of concept in GH-20481

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Mark Dickinson


Change by Mark Dickinson :


--
keywords: +patch
pull_requests: +19730
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/20481

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue40801] Expose PyFloat_ToDouble at Python level: operator.as_float?

2020-05-28 Thread Mark Dickinson


Change by Mark Dickinson :


--
title: Expose PyFloat_ToDouble at Python level: operator.to_float? -> Expose 
PyFloat_ToDouble at Python level: operator.as_float?

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com