[Python-Dev] boxing and unboxing data types

Serhiy Storchaka storchaka at gmail.com
Mon Mar 9 10:10:09 CET 2015
On 09.03.15 10:19, Maciej Fijalkowski wrote:
> Not all your examples are good.
>
> * float(x) calls __float__ (not __int__)
>
> * re.group requires __eq__ (and __hash__)
>
> * I'm unsure about OSError
>
> * the % thing at the very least works on pypy

Yes, all these examples are implementation defined and can differ 
between CPython and PyPy. There is about a dozen of similar examples 
only in C part of CPython. Most of them have in common is that the 
behavior of the function depends on the argument type. For example in 
case of re.group an argument is either integer index or string group 
name. OSError constructor can produce OSError subtype if first argument 
is known integer errno. float either convert a number to float or parse 
a string (or bytes).

Python functions can be more lenient (if they allows ducktyping) or more 
strict (if they explicitly check the type). They rarely call __index__ 
or __int__.



More information about the Python-Dev mailing list