<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Apr 1, 2013 at 12:28 AM, Mark Dickinson <span dir="ltr"><<a href="mailto:dickinsm@gmail.com" target="_blank">dickinsm@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">As written, int_check would do the wrong thing for bools, too:  I definitely want int(True) to be 1, not True.
<div><br></div><div>For (2) and (4), it's not so clear.  Are there use-cases for an __index__ return value that's not directly of type int?  I can't think of any offhand.</div></div></div></div></blockquote><div>

<br></div><div>int() and operator.index() are both type coercion calls to produce true Python integers - they will never return a subclass, and this is both deliberate and consistent with all the other builtin types that accept an instance of themselves as input to the constructor. Passing a subclass instance to the base class constructor is the way you convert a subclass to an ordinary instance of the base class:<br>

<br>>>> for base in (str, bytes, bytearray, int, float, complex, dict, tuple, list, set, frozenset):<br>...     class subclass(base): pass<br>...     print("'type(base(subclass()))' is", type(base(subclass())))<br>

...                                                                                                   <br>'type(base(subclass()))' is <class 'str'>                                                             <br>

'type(base(subclass()))' is <class 'bytes'>                                                           <br>'type(base(subclass()))' is <class 'bytearray'>                                                       <br>

'type(base(subclass()))' is <class 'int'>                                                             <br>'type(base(subclass()))' is <class 'float'>                                                           <br>

'type(base(subclass()))' is <class 'complex'>                                                         <br>'type(base(subclass()))' is <class 'dict'><br>'type(base(subclass()))' is <class 'tuple'><br>

'type(base(subclass()))' is <class 'list'><br>'type(base(subclass()))' is <class 'set'><br>'type(base(subclass()))' is <class 'frozenset'><br><br>There's code in the slot wrappers so that if you return a non-int object from either __int__ or __index__, then the interpreter will complain about it, and if you return a subclass, it will be stripped back to just the base class.<br>

</div><div><br></div><div>If the language and library reference aren't clear on this, it's a documentation issue.<br><br></div><div>Cheers,<br>Nick.<br></div><div><br></div></div>-- <br>Nick Coghlan   |   <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>   |   Brisbane, Australia
</div></div>