Well I would expect that it doesn't affect the contents of the container, but I wouldn't be too surprised if it did some processing on the container that takes a while to deduce whether it is empty or not, which could affect the performance charecteristics.
The best example I could find in the standard library is collections.ChainMap, which combines multiple dicts or mappings into one. Checking whether it is empty requires checking each of the underlying mappings in turn to see if they are empty. Thanks to processor optimisations, this can lead to a significant performance difference in later code.
Ah, I guess that's another assumption I would have: speed. Implicit bool conversion being the bottleneck would be a tough bug to track down. Again, I might have a non-dunder method for that, `calculate_is_empty` or something.
View all comments
Show parent comment
1
u/[deleted] Mar 05 '26
Well I would expect that it doesn't affect the contents of the container, but I wouldn't be too surprised if it did some processing on the container that takes a while to deduce whether it is empty or not, which could affect the performance charecteristics.
The best example I could find in the standard library is collections.ChainMap, which combines multiple dicts or mappings into one. Checking whether it is empty requires checking each of the underlying mappings in turn to see if they are empty. Thanks to processor optimisations, this can lead to a significant performance difference in later code.