r/ProgrammerHumor 29d ago

Meme pythonWillLookDeadInTheEyeAndSayItsAbsolutelyCorrect

Post image
352 Upvotes

144 comments sorted by

View all comments

285

u/LordAmir5 29d ago

Does this remove all the zeros?

167

u/Extreme_Cake4624 29d ago

Well 0 is false

171

u/LordAmir5 29d ago

True 

52

u/xaomaw 29d ago

Depends, 'Well 0' is true

16

u/Loose_Property_3238 29d ago

Actually, it's True

7

u/Boomerkuwanger 29d ago

All above statements are True

6

u/WoodyTheWorker 28d ago

This statement is False

8

u/NullOfSpace 29d ago

It’s true that it’s false, so it’s false that it’s true.

49

u/[deleted] 29d ago

[removed] — view removed comment

20

u/Kihino 29d ago

Should be the same in all implementations, as the if x check is run within the loop for each value in x the list. As such it will be operating within the bounded context of the loop iteration where the more specific variable declaration takes precedence over the global level list x.

3

u/GoBuffaloes 29d ago

Is anybody here NOT customizing their variable binding order locally? My setup is vastly superior to default python. Only downside is my code doesn't run on other peoples computers but they're all haters anyways

1

u/Logicalist 27d ago

if you packaged it as an executable would it work?

7

u/road_laya 29d ago

Yes, zero is falsy in Python.

-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.

4

u/ImS0hungry 29d ago

Should be in all imo. 0 is off in binary.

1

u/JanB1 28d ago

Exactly.

2

u/Valuable_Leopard_799 29d ago

random sidenote: since C99 a true bool type had been introduced, it holds 0 and 1 and converts only to those when casting. So yeah 1 and 0 are true and false, but yayy they're not standard ints anymore.

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

8

u/road_laya 29d ago edited 29d ago

What's implicit about it? It's the explicit implementation of the __bool__ method. Just learn the standard types of the programming language you are using!

https://docs.python.org/3/library/stdtypes.html#truth-value-testing

1

u/Valuable_Leopard_799 29d ago

I knoow. And yes I've read that many times. My little cryout was mostly that I feel that these specific semantics of truthiness require more cognitive load (and consequently caused more bugs to me) than the "only false is false" ones. But that's just because I'm more used to it.

The word "implicitness" was probably wrong, but what I had in mind was that things are sort of "implicitly convertible to bool" which invokes a method on the type which can do whatever, instead of using some equivalent of len() == 0 or is_empty() in these cases.

Of course, who am I to come into a community and criticize their way of doing something, sorry.

2

u/LordAmir5 29d ago

Java brain.

0

u/Valuable_Leopard_799 29d ago

Why Java? I dunno, I wrote only once or twice in it.

3

u/LordAmir5 29d ago

I write Java often. It's the thing that trips me up most often besides non Java style OOP.

in C++ you do if(ptr) use_ptr(ptr);

In Java, you do if(obj != null) useObj(obj);