A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2013-April/125134.html below:

[Python-Dev] Semantics of __int__(), __index__()

[Python-Dev] Semantics of __int__(), __index__()Stefan Behnel stefan_ml at behnel.de
Fri Apr 5 08:22:12 CEST 2013
Guido van Rossum, 04.04.2013 23:14:
> On Thu, Apr 4, 2013 at 1:50 PM, Tim Delaney wrote:
>> I fall into:
>>
>> 1. int(), float(), str() etc should return that exact class (and
>> operator.index() should return exactly an int).
>>
>> 2. It could sometimes be useful for __int__() and __index__() to return a
>> subclass of int.
>>
>> So, for the int constructor, I would have the following logic (assume
>> appropriate try/catch):
>>
>> def __new__(cls, obj):
>>     i = obj.__int__()
>>
>>     if type(i) is int:
>>         return i
>>
>>     return i._internal_value
> 
> CPython can solve this in C using an unsafe cast, and the code that
> checks for allowable subclasses of int actually ensures such a cast
> will work. But it still feels wrong; __int__ should be expected to do
> the work.

+1, that's why it's called "__int__" (even if it returns a PyLong in Py3 ;)

Stefan


More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4