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

Steven D'Aprano steve at pearwood.info
Wed Apr 3 19:41:27 CEST 2013

On 04/04/13 03:36, Guido van Rossum wrote:

> Consider a subclass of int() that overrides __repr__() and __str__() to
> print something fancy (maybe it defaults to hex; maybe it's an enum :-). I
> want to be able to say repr(int(x)) and get the standard decimal
> representation. Same with strings. If int() or str() were allowed to return
> a subclass instance, this wouldn't work, and I'd have to resort to
> draconian measures.

int and str currently are allowed to return subclass instances.

> (Betcha the first few solutions you come up with don't
> even work. :-)

Well, I'm always game to learn something. Challenge accepted.

# Force a string subclass s to a built-in string.

# Force an int subclass n to a built-in int.

# And similarly for float subclass.


More information about the Python-Dev mailing list