[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Steven D'Aprano
On Thu, Oct 14, 2021 at 11:52:11AM +1300, Greg Ewing wrote:

> Scratch that, it seems __trunc__ also returns an int, at least
> for floats. Not sure what the logic behind that is.

I'm not sure about the logic either, but it is documented as returning 
an Integral:

"Truncates the Real x to the nearest Integral toward 0."

so the option is there for third-party types to return some integral 
type apart from int. For the stdlib, the only Integral type we have is 
int. So I think we have the following intended behaviour.

* Round a numeric (Real) value to an Integral value:

  - round to nearest (ties to even): __round__

  - round down (towards negative infinity): __floor__

  - round up (towards positive infinity): __ceil__

  - round towards zero: __trunc__

* Convert a numeric Integral value to an actual int: (intended for 
  indexing of sequences): __index__

* Convert any arbitrary value to an actual int: __int__

Does that seem right?


-- 
Steve
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/TFKQPKGUU3G2HI5AVIFFKSULU3YCLLRY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Antoine Pitrou
On Thu, 14 Oct 2021 11:52:11 +1300
Greg Ewing  wrote:

> On 14/10/21 11:19 am, Greg Ewing wrote:
> > Not really -- __int__ is expected to return something of type
> > int, whereas __trunc__ is expected to return the same type as
> > its operand.  
> 
> Scratch that, it seems __trunc__ also returns an int, at least
> for floats. Not sure what the logic behind that is.
> 
> There are differences between the functions int() and trunc()
> though:
> 
>  >>> int("42")  
> 42
> 
>  >>> trunc("42")  
> Traceback (most recent call last):
>File "", line 1, in 
> TypeError: type str doesn't define __trunc__ method

That is behind the point, because str doesn't define __int__ either.
The int() function does more than just call __int__, it checks a whole
lot of different possibilites (including checks for str and buffer-like
objects, indeed).

Similarly:

>>> int(memoryview(b"123"))
123
>>> memoryview(b"123").__int__()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'memoryview' object has no attribute '__int__'

Regards

Antoine.


___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/IRRRKKFXCS64LXXJF5TQPBF5MX4UOTIE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Guido van Rossum
On Wed, Oct 13, 2021 at 4:56 PM Victor Stinner  wrote:

> Honestly, I don't understand well the difference between __int__() and
> __index__().
>
> * https://docs.python.org/dev/reference/datamodel.html#object.__int__
> * https://docs.python.org/dev/reference/datamodel.html#object.__index__
>

If you want to index a list or array 'a' with index 'i', and i is not an
int already, we try to convert it to int using __index__. This should fail
for floats, since a[3.14] is a bug. OTOH, int(x) where x is a float should
work, and that's where __int__ is used. And int(s) where s is a string
should also work, so int() can't call __trunc__ (as was explained earlier
in the thread).

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/DDADEDENVVWC3JJNAR4LS6WGUUC63EUS/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Chris Angelico
On Thu, Oct 14, 2021 at 10:51 AM Victor Stinner  wrote:
>
> Honestly, I don't understand well the difference between __int__() and
> __index__().
>
> * https://docs.python.org/dev/reference/datamodel.html#object.__int__
> * https://docs.python.org/dev/reference/datamodel.html#object.__index__

__int__ is for converting to integer, __index__ is for interpreting as
integer. The intention of __index__ is that it already has that exact
value, whereas __int__ might be rounding. For instance, int(5.25) is
5, but operator.index(5.25) raises.

Quoting from that linked page on __index__:
"Called ... whenever Python needs to **losslessly** convert the
numeric object to an integer object"

It is assumed to already be a numeric type (so "5" isn't, even though
it could be cast to int), and the conversion should be lossless.

I'm not 100% sure, but I think that, if __index__ returns anything,
__int__ should return the same thing. There could be edge cases where
that's not true though.

ChrisA
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/WZSTHNTNZWHIKXWK2OOQK3MYI6PGPBJ5/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Victor Stinner
Hi Antoine,

I have a lot of troubles to reminder how Python converts numbers, I
collected notes about the Python "number tower" and the C
implementation:
https://pythondev.readthedocs.io/numbers.html

Honestly, I don't understand well the difference between __int__() and
__index__().

* https://docs.python.org/dev/reference/datamodel.html#object.__int__
* https://docs.python.org/dev/reference/datamodel.html#object.__index__

Victor

On Wed, Oct 13, 2021 at 7:11 PM Antoine Pitrou  wrote:
>
>
> Hello,
>
> It used to be that defining __int__ allowed an object to be accepted as
> an integer from various functions, internal and third-party, thanks to
> being implicitly called by e.g. PyLong_AsLong.
>
> Today, and since bpo-37999, this is no longer the case.  It seems that
> __int__ has now become a strict equivalent to __trunc__.  Of course,
> user code can still look up and call the __int__ method explicitly (or
> look up the nb_int slot, in C), but that's a bit of anti-pattern.
>
> Is there a point in having three special methods __index__, __int__ and
> __trunc__, if two are them are practically interchangeable?
>
> Regards
>
> Antoine.
>
>
> ___
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-dev@python.org/message/K6TEYMDY5NEDV4MHH6EGIOQWDOAKSPJV/
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
Night gathers, and now my watch begins. It shall not end until my death.
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/QFMFPMCSDWV7S5QGPYS7K2VN4CHF5KDL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Greg Ewing

On 14/10/21 11:19 am, Greg Ewing wrote:

Not really -- __int__ is expected to return something of type
int, whereas __trunc__ is expected to return the same type as
its operand.


Scratch that, it seems __trunc__ also returns an int, at least
for floats. Not sure what the logic behind that is.

There are differences between the functions int() and trunc()
though:

>>> int("42")
42

>>> trunc("42")
Traceback (most recent call last):
  File "", line 1, in 
TypeError: type str doesn't define __trunc__ method

Conceptually, I would say that int() is a type conversion,
whereas trunc() is an operation on numbers. A type would be
entitled to implement them both but differently.

--
Greg

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/OM5UB5LRMOSKTVGJWV3G7TP7Z7DNFE2M/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Greg Ewing

On 14/10/21 6:10 am, Antoine Pitrou wrote:

It seems that
__int__ has now become a strict equivalent to __trunc__.


Not really -- __int__ is expected to return something of type
int, whereas __trunc__ is expected to return the same type as
its operand.

--
Greg
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/5XD26XS2WW7H2I3KZBY5EZLNV64RX6VM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: What is __int__ still useful for?

2021-10-13 Thread Serhiy Storchaka
13.10.21 20:10, Antoine Pitrou пише:
> It used to be that defining __int__ allowed an object to be accepted as
> an integer from various functions, internal and third-party, thanks to
> being implicitly called by e.g. PyLong_AsLong.
> 
> Today, and since bpo-37999, this is no longer the case.  It seems that
> __int__ has now become a strict equivalent to __trunc__.  Of course,
> user code can still look up and call the __int__ method explicitly (or
> look up the nb_int slot, in C), but that's a bit of anti-pattern.
> 
> Is there a point in having three special methods __index__, __int__ and
> __trunc__, if two are them are practically interchangeable?

Today __int__ allows the object be explicitly converted to int. It is
defined for example in UUID and IPv4Address. We do not want them to be
converted to int implicitly or be valid argument of math.trunc().

__trunc__ adds support of the type in math.trunc(). There is no
requirement that it should return an int. GMP numbers can return GMP
integers and NumPy arrays can return NumPy arrays (I do not know whether
they do). It is similar to __floor__, __ceil__ and __round__.

___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/IW4TTWZ2LGZOYUST2CA4X5HXM4XNWA72/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Heads up: core sprint coming up!

2021-10-13 Thread Łukasz Langa
Hi there, I have a reminder for you if you are:
- a core developer; or
- on the triage team; or
- a mentee in core-mentorship.

We are running an online core sprint next week (October 18 - 24). "Online" 
means on our internal Core Python Discord. Join us there to plan and chat! You 
can even get a meal stipend from the PSF for the duration of the sprint! If you 
need a Discord invite, write to me directly and I'll sort you out 

What if you'd like to participate but you're not a core dev, triager, nor 
mentee? Well, this is a closed event to let our team coordinate in real time. 
We'd do this one in person if that was possible. In fact, this is how we have 
done it between 2016 and 2019. The goals don't change though: to have a small 
event separate from major conferences and free from distractions so we can 
focus on discussing key changes and making larger code changes. Instead, come 
to the sprints at PyCon US! And in the mean time, I suggest you pick up bugs on 
bugs.python.org  and make pull requests. I'll review 
it personally if you'd like 邏

--
Łukasz Langa
CPython Developer in Residence
Python Software Foundation



signature.asc
Description: Message signed with OpenPGP
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/ETBKP4OPZSFQRCB2W7T4KFLJI2DGDI4F/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] What is __int__ still useful for?

2021-10-13 Thread Antoine Pitrou


Hello,

It used to be that defining __int__ allowed an object to be accepted as
an integer from various functions, internal and third-party, thanks to
being implicitly called by e.g. PyLong_AsLong.

Today, and since bpo-37999, this is no longer the case.  It seems that
__int__ has now become a strict equivalent to __trunc__.  Of course,
user code can still look up and call the __int__ method explicitly (or
look up the nb_int slot, in C), but that's a bit of anti-pattern.

Is there a point in having three special methods __index__, __int__ and
__trunc__, if two are them are practically interchangeable?

Regards

Antoine.


___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/K6TEYMDY5NEDV4MHH6EGIOQWDOAKSPJV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-Dev] Re: Python multithreading without the GIL

2021-10-13 Thread Simon Cross
> Still, I hope you at least enjoyed my enthusiasm!

I did!
___
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/BJFDVRCZMEDOHEMCCIJJP6NTX6HOGC5L/
Code of Conduct: http://python.org/psf/codeofconduct/