On Wed, Jan 13, 2021 at 9:08 PM Emily Bowman <silverback...@gmail.com> 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? >
Yes - just say "will be called when the interpreter needs to know the booleanness of the object", and don't mandate that it be called if the interpreter already knows. If someone writes a __bool__ function that decrements a counter and returns True if it's still above zero, then they're shooting themselves in the foot, and we don't have to protect them. It's like having __iter__ return an object on which __iter__ doesn't return self. There's no protection in the language to stop you from creating such a monster, but you'll run into problems sooner or later if you do, and it's not a bug in Python. ChrisA _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/RN7SAZ3X4YM2KEHNVMAXDNPLQFWVY7NJ/ Code of Conduct: http://python.org/psf/codeofconduct/