On 2013-04-03, at 19:46 , Barry Warsaw wrote: > On Apr 04, 2013, at 03:04 AM, Steven D'Aprano wrote: > >> On 04/04/13 01:16, Barry Warsaw wrote: > >>> the other built-in types-as-functions, so int() calls __int__() which must >>> return a concrete integer. > >> Why must it? I think that's the claim which must be justified, not just taken >> as a given. When we call n = int(something), what's the use-case for caring >> that n is an instance of built-in int but not of a subclass, and is that >> use-case so compelling that it must be enforced for all uses of int() etc.? > > It's a consistency-of-implementation issue. Where built-in types are > callable, they return concrete instances of themselves. This is true for > e.g. list, tuple, dict, bytes, str, and should also be true of int.
FWIW unless I missed something it's true for none of bytes, str or float, though it's true for complex (for some reason): types = (int, float, complex, bytes, str) Obj = type('Obj', (), { '__{0.__name__}__'.format(t): (lambda t: lambda self: type('my_{0.__name__}'.format(t), (t,), {})())(t) for t in types }) obj = Obj() for t in types: print("{} = {} ? {}".format(t, type(t(obj)), type(t(obj)) is t)) > python3 test.py <class 'int'> = <class '__main__.my_int'> ? False <class 'float'> = <class '__main__.my_float'> ? False <class 'complex'> = <class 'complex'> ? True <class 'bytes'> = <class '__main__.my_bytes'> ? False <class 'str'> = <class '__main__.my_str'> ? False bool can not be subclassed so the question doesn't make sense for it Broadly speaking (complex doesn't fit it), if there's a dedicated dunder method in the data model, the only check on what it returns is that it's a subtype of the conversion type. list, tuple and dict use non-dedicated conversion methods (iteration or a fallback thereof) so they don't have this occasion and have no choice but to instantiate "themselves" _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com