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

Steven D'Aprano steve at pearwood.info
Thu Apr 4 04:46:25 CEST 2013

On 04/04/13 09:07, Cameron Simpson wrote:
> On 03Apr2013 14:47, Hrvoje Niksic <hrvoje.niksic at avl.com> wrote:
> | On 04/03/2013 01:17 PM, Nick Coghlan wrote:
> | Why would one want to be absolutely sure of getting an int?
> So that arithmetic can be relied upon? If a subclass can override
> the add methods etc it can look like an int, be a subclass instance
> of an int, and yet not act like an int in all ways.

Python is generally a consenting adults language. If you don't trust the subclass to actually behave like an int in ways that matter, why are you using it?

Ultimately, something may have monkey-patched builtins int without your knowledge, so even calling int() doesn't guarantee anything about the object you get back.

Isn't it funny how we have differences in opinion on just when and how we're allowed to shoot ourselves in the foot? I personally hate the idea that global constants aren't constant, and can be rebound by anything, but others apparently don't care. You hate the idea that int() might return an instance of a subclass, and I think that's a feature, not a bug.


More information about the Python-Dev mailing list