News for hackers. Stuff that matters.
Re:Evil Coding Incantations by ubernostrum on Monday December 25, 2017 @11:09PM (#16007024)
You should get a type error if you try to use an int in boolean logic.

So define:

    def __bool__(self):
        raise TypeError("I don't like non-bool booleans")
on your classes, and you're good to go. Python isn't doing type conversion, it's doing operator overloading and letting you define how your class should work with the 'if' statement. It won't help you with the already-defined behavior of built-in and third-party classes, but you can have all your own code be unusable in boolean operations if you like.

There are no comments on this comment yet.