avi.e.gr...@gmail.com wrote:
...
Interestingly, I wonder if anyone has
designed an alternate object type that can be used mostly in place of
Booleans but which imposes changes and restrictions so trying to add a
Boolean to an integer, or vice versa, results in an error. Python is
flexible enough to do that and perhaps there already is a module out there

Not exactly what you describe, but I did once write a subclass of int for use in an SNMP interface, where true is represented as 1 and false as 2. I don't recall the exact details offhand, but it involved overriding __bool__ so that a value of 1 returned True and anything else False. The way it was used should have ensured that it only ever had the value 1 or 2, but doing it this way round (rather than e.g. 2 being False and anything else True) meant that if it somehow got the value 0, that would also be treated as False. I think it also overrode __init__ (or perhaps __new__) to covert a bool True or False to 1 or 2 (rather than 1 or 0) for its own value, so it could be initialised from either an int or a bool and correctly converted in either direction via int() or bool().

So Python is even flexible enough to be made to deal with insane situations where False is 2!

--
Mark.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to