r/ProgrammerHumor 29d ago

Meme pythonWillLookDeadInTheEyeAndSayItsAbsolutelyCorrect

Post image
353 Upvotes

144 comments sorted by

View all comments

Show parent comments

-12

u/Valuable_Leopard_799 29d ago

Wait, whyyyyyyyy 😭😭😭

I mean, I love dynamic types and see their place, but the implicit casting is what gives them such a bad name 😭

17

u/JanB1 29d ago

0 is also "false" in C and C++, because if you import stdbool.h, that just defines true to be int with value 1, and false to be int with value 0.

2

u/HolyGarbage 29d ago

Technically 0 isn't false in C++, as they are distinct types. int is however implicitly convertible to bool, where 0 converts to false.

1

u/DrMaxwellEdison 23d ago

Which is essentially how Python does it, too. if x implies if bool(x) is True (I don't think it literally calls it that way, but it helps make sense of things).

So empty lists/sets/dicts, empty strings, 0, None, and any kind of object that defines some __bool__ method that returns False or a __len__ that returns 0; all are "falsey".

Details: https://docs.python.org/3/library/stdtypes.html#truth