Christopher Barker wrote:
Surely what you're looking for is some kind of typed hash table? Maybe, maybe not. My impression is that the Typed hash table is a kluge to
get around this one issue. As others have pointed out, 1 and 1.0 are considered equal, and thus hash the same — a typed hash table would not consider them the same. And while maybe useful, it would violate the spirit of duck typing. If you really want that kind of static typing, maybe Python is not the language for the job.
The OP states that in their case True and 1 should _not_ hash the same, whether that gels with Python was not my point, I was saying that this use case, to me, is asking for some kind of typed hash table not a change to the typing of Booleans themselves.
(And since booleans are subclasses of Ints, they might still class in a typed has table, depending on the implementation)
Hence my suggestion that the OP's solution of a bespoke key mapping to tuples is not so bad (i.e. not making it part of the stdlib), but that it may be nicer to deal with if it is abstracted away into the getitem, setitem ... dunder methods.
What I think the OP wants, and I happen to agree, is for Booleans to not only not BE integers in the subclassing sense, but also to not behave as numbers in the. Duck Typing sense.
Apologies, but that is not what the user is saying, they are saying arguably the opposite. I am not saying I agree with the OP on this, but I don't think the behaviour of Bools in mathematical operations is their point.
To the OP: perhaps you could create your own non-integer Bool type, and then put a wrapper around set and/or dict, that substitutes it on insertion. Whether that is more or less kludgy than your solution depends on your use case.
I think this is a similar idea to what I was suggesting but limited to only handle Booleans.