Hello,
On Thu, 14 Jan 2021 03:29:35 +1100
Steven D'Aprano
On Wed, Jan 13, 2021 at 02:08:01AM -0800, Emily Bowman wrote:
Even if you define __bool__() as returning a bool, and error/undefined behavior otherwise, that doesn't eliminate side effects. Is it even possible to nail down a definition to the point that you can say, "Thou shalt not mutate or cause anything" and have it meaningfully enforced in the compiler or interpreter?
No, I don't think it is possible to enforce lack of side-effects. Not without rebuilding the language from the ground up with a clear, definitive and enforcable distinction between pure and impure functions.
(I think Haskell does something like that. But trying to retrofit that into Python would probably be about as hard as building a restricted mode, and for similar reasons.)
Besides, we probably don't want to prohibit side-effects in `__bool__`. That would prohibit useful tricks such as putting logging calls into a method you are trying to debug.
Surely, if we do that, we wouldn't use Haskell's definition of purity ;-). Rather, a practical definition of purity, Python-style. For example, print() would be considered "pure", as its purpose is to provide program output, not arbitrarily change program state (so, all of __str__, __repr__, __format__ would need to be pure, but if someone overrode print() to shoot at random modules/data at them, then they shoot themselves in the feet, nothing else). [] -- Best regards, Paul mailto:pmiscml@gmail.com