Yea and then you use “not” with a variable name that does not make it obvious that it is a list and another person who reads the code thinks it is a bool. Hell a couple of months later you yourself wont even understand that it is a list. Moreover not will not throw an error if you don’t use an iterable there as you should but len will.
You should not sacrifice code readability and safety for over optimization, this is phyton after all I don’t think list lengths will be your bottle neck.
defdo_work(foo: list | None):
ifnot foo:
return
...
Boom, self-documenting, faster, and very simple.
len(foo) == 0 also doesn’t imply it’s a list, it could be a dict or any other type that implements the __len__. That matters a lot in most cases, so I highly recommend using type hints instead of relying on assumptions like len(foo) == 0 is probably a list operation.
Well, in your case it is not clear whether you intended to branch in the variable foo being None, or on the list being empty which is semantically very different…
Thats why it’s better to explicitly express whether you want an empty collection (len = 0) or a None value.
I don’t really understand the point about exceptions. Yeah “not foo” cannot throw an exception. But the program should crash if an invalid input is provided. If the function expects an optional[list] it should be provided with either a list or None, nothing else.
Sure. But is None invalid input in your case, whereas []is valid? If so, make that check explicit, don’t rely on an implicit check that len(...) does.
When I see TypeError in the logs, I assume the developer screwed up. When I see ValueError in the logs, I assume the user screwed up. Ideally, TypeError should never happen, and every case where it could happen should transform it to another type of exception that indicates where the error actually lies.
The only exceptions I want to see in my code are:
exceptions from libraries, such as databases and whatnot, when I do something invalid
explicitly raised exceptions
Implicit ones like accessing attributes on None or calling methods that don’t exist shouldn’t be happening in production code.
I agree. So if None is a valid input we should check it first, and then check if the length is zero.
In this situation, we see a type error only if the programmer screwed up and everything is explicit
Yes. If None is just as valid and has the same meaning as [] for the function (true more often than not), just do if not foo. If None should be handled separately from [] for some reason, treat them both separately so it’s absolutely clear.
Explicit is better than implicit.
Errors should never pass silently.
And I especially like this one:
That said, jihadists are a subset of Nazis, just a not very stereotypical one for a westerner.
The one obvious way to check if you have data is if foo. That works for pretty much everything as you’d expect. Explicitly deviating from that is a cue to the reader that they should pay attention. In this case, that means None is semantically different than empty data, and that’s something the reader should be aware of because that’s usually not the case.
I dislike treating None as an equivalent for the empy list, but that does not further the discussion…
I hurt myself in confusion while reading the second quote. Is it the right quote? (also, nazi (relating to the nsdap) is probably not the right word, did you mean fascist?)
There is no guarantee that the comment is kept up to date with the code. “Self documenting code” is a meme, but clearly written code is pretty much always preferable to unclear code with a comment, largely because you can actually be sure that the code does what it says it does.
If there is an alternative through which I can achieve the same intended effect and is a bit more safer (because it will verify that it has len implemented) I would prefer that to commenting. Also if I have to comment every len use of not that sounds quite redundant as len checks are very common
Comments shouldn’t explain code. Code should explain code by being readable.
Comments are for whys. Why is the code doing the things it’s doing. Why is the code doing this strange thing here. Why does a thing need to be in this order. Why do I need to store this value here.
In my experience, if you didn’t write the function that creates the list, there’s a solid chance it could be None too, and if you try to check the length of None, you get an error. This is also why returning None when a function fails is bad practice IMO, but that doesn’t seem to stop my coworkers.
i haven’t programmed since college 15 years ago and even i know that 0 == false for non bool variables. what kind of professional programmers wouldn’t know that?
I would say it depends heavily on the language. In Python, it’s very common that different objects have some kind of Boolean interpretation, so assuming that an object is a bool because it is used in a Boolean context is a bit silly.
if not x then … end is very common in Lua for similar purposes, very rarely do you see hard nil comparisons or calls to typeof (last time I did was for a serializer).
Well fair enough but I still like the fact that len makes the aim and the object more transparent on a quick look through the code which is what I am trying to get at. The supporting argument on bools wasn’t’t very to the point I agree.
That being said is there an application of “not” on other classes which cannot be replaced by some other more transparent operator (I confess I only know the bool and length context)? I would rather have transparently named operators rather than having to remember what “not” does on ten different types. I like duck typing as much as the next guy, but when it is so opaque as in the case of not, I prefer alternatives.
For instance having open or read on different objects which does really read or open some data vs not some object god knows what it does I should memorise each case.
Truthiness is so fundamental, in most languages, all values have a truthiness, whether they are bool or not. Even in C, int x = value(); if (!x) x_is_not_zero(); is valid and idiomatic.
I appreciate the point that calling a method gives more context cues and potentially aids readability, but in this case I feel like not is the python idiom people expect and reads just fine.
I don’t know, it throws me off but perhaps because I always use len in this context. Is there any generally applicable practical reason why one would prefer “not” over len? Is it just compactness and being pythonic?
It’s very convenient not to have to remember a bunch of different means/methods for performing the same conceptual operation. You might call len(x) == 0 on a list, but next time it’s a dict. Time after that it’s a complex number. The next time it’s an instance. not works in all cases.
I feel like that only serves the purpose up to the point that methods are not over reaching otherwise then it turns into remembering what a method does for a bunch of unrelated objects.
I definitely agree that len is the preferred choice for checking the emptiness of an object, for the reasons you mention. I’m just pointing out that assuming a variable is a bool because it’s used in a Boolean context is a bit silly, especially in Python or other languages where any object can have a truthiness value, and where this is commonly utilised.
It is not “assume” as in a conscious “this is probably a bool I will assume so” but more like a slip of attention by someone who is more used to the bool context of not. Is “not integer” or “not list” really that commonly used that it is even comparable to its usage in bool context?
Yea and then you use “not” with a variable name that does not make it obvious that it is a list and another person who reads the code thinks it is a bool. Hell a couple of months later you yourself wont even understand that it is a list. Moreover not will not throw an error if you don’t use an iterable there as you should but len will.
You should not sacrifice code readability and safety for over optimization, this is phyton after all I don’t think list lengths will be your bottle neck.
That’s why we use type-hinting at my company:
def do_work(foo: list | None): if not foo: return ...
Boom, self-documenting, faster, and very simple.
len(foo) == 0
also doesn’t imply it’s alist
, it could be adict
or any other type that implements the__len__
. That matters a lot in most cases, so I highly recommend using type hints instead of relying on assumptions likelen(foo) == 0
is probably a list operation.Well, in your case it is not clear whether you intended to branch in the variable foo being None, or on the list being empty which is semantically very different…
Thats why it’s better to explicitly express whether you want an empty collection (len = 0) or a None value.
Well yeah, because I’m explicitly not defining a difference between
None
and[]
. In most cases, the difference doesn’t matter.If I did want to differentiate, I’d use another
if
block:if foo is None: ... if not foo: ...
Explicit is better than implicit. I hate relying on exceptions like
len(foo) == 0
raising aTypeError
because that’s very much not explicit.Exceptions should be for exceptional cases, as in, things that aren’t expected. If it is expected, make an explicit check for it.
I don’t really understand the point about exceptions. Yeah “not foo” cannot throw an exception. But the program should crash if an invalid input is provided. If the function expects an optional[list] it should be provided with either a list or None, nothing else.
Sure. But is
None
invalid input in your case, whereas[]
is valid? If so, make that check explicit, don’t rely on an implicit check thatlen(...)
does.When I see
TypeError
in the logs, I assume the developer screwed up. When I seeValueError
in the logs, I assume the user screwed up. Ideally,TypeError
should never happen, and every case where it could happen should transform it to another type of exception that indicates where the error actually lies.The only exceptions I want to see in my code are:
Implicit ones like accessing attributes on
None
or calling methods that don’t exist shouldn’t be happening in production code.I agree. So if None is a valid input we should check it first, and then check if the length is zero. In this situation, we see a type error only if the programmer screwed up and everything is explicit
Yes. If
None
is just as valid and has the same meaning as[]
for the function (true more often than not), just doif not foo
. IfNone
should be handled separately from[]
for some reason, treat them both separately so it’s absolutely clear.And I especially like this one:
The one obvious way to check if you have data is
if foo
. That works for pretty much everything as you’d expect. Explicitly deviating from that is a cue to the reader that they should pay attention. In this case, that meansNone
is semantically different than empty data, and that’s something the reader should be aware of because that’s usually not the case.I dislike treating None as an equivalent for the empy list, but that does not further the discussion…
I hurt myself in confusion while reading the second quote. Is it the right quote? (also, nazi (relating to the nsdap) is probably not the right word, did you mean fascist?)
if you’re worried about readability you can leave a comment.
There is no guarantee that the comment is kept up to date with the code. “Self documenting code” is a meme, but clearly written code is pretty much always preferable to unclear code with a comment, largely because you can actually be sure that the code does what it says it does.
Note: You still need to comment your code kids.
If there is an alternative through which I can achieve the same intended effect and is a bit more safer (because it will verify that it has len implemented) I would prefer that to commenting. Also if I have to comment every len use of not that sounds quite redundant as len checks are very common
Comments shouldn’t explain code. Code should explain code by being readable.
Comments are for whys. Why is the code doing the things it’s doing. Why is the code doing this strange thing here. Why does a thing need to be in this order. Why do I need to store this value here.
Stuff like that.
In my experience, if you didn’t write the function that creates the list, there’s a solid chance it could be
None
too, and if you try to check the length ofNone
, you get an error. This is also why returningNone
when a function fails is bad practice IMO, but that doesn’t seem to stop my coworkers.good point I try to initialize None collections to empty collections in the beginning but not always guaranteed and len would catch it
Strongly disagree that
not x
implies to programmers thatx
is a bool.i haven’t programmed since college 15 years ago and even i know that 0 == false for non bool variables. what kind of professional programmers wouldn’t know that?
well it does not imply directly per se since you can “not” many things but I feel like my first assumption would be it is used in a bool context
You can make that assumption at your own peril.
I don’t think they are a minority
If anything len tells you that it is a sequence or a collection, “not” does not tell you that. That I feel like is the main point of my objection.
I would say it depends heavily on the language. In Python, it’s very common that different objects have some kind of Boolean interpretation, so assuming that an object is a bool because it is used in a Boolean context is a bit silly.
if not x then … end
is very common in Lua for similar purposes, very rarely do you see hard nil comparisons or calls totypeof
(last time I did was for a serializer).Well fair enough but I still like the fact that len makes the aim and the object more transparent on a quick look through the code which is what I am trying to get at. The supporting argument on bools wasn’t’t very to the point I agree.
That being said is there an application of “not” on other classes which cannot be replaced by some other more transparent operator (I confess I only know the bool and length context)? I would rather have transparently named operators rather than having to remember what “not” does on ten different types. I like duck typing as much as the next guy, but when it is so opaque as in the case of not, I prefer alternatives. For instance having open or read on different objects which does really read or open some data vs not some object god knows what it does I should memorise each case.
Truthiness is so fundamental, in most languages, all values have a truthiness, whether they are bool or not. Even in C,
int x = value(); if (!x) x_is_not_zero();
is valid and idiomatic.I appreciate the point that calling a method gives more context cues and potentially aids readability, but in this case I feel like
not
is the python idiom people expect and reads just fine.I don’t know, it throws me off but perhaps because I always use len in this context. Is there any generally applicable practical reason why one would prefer “not” over len? Is it just compactness and being pythonic?
It’s very convenient not to have to remember a bunch of different means/methods for performing the same conceptual operation. You might call
len(x) == 0
on a list, but next time it’s a dict. Time after that it’s a complex number. The next time it’s an instance.not
works in all cases.I feel like that only serves the purpose up to the point that methods are not over reaching otherwise then it turns into remembering what a method does for a bunch of unrelated objects.
I definitely agree that
len
is the preferred choice for checking the emptiness of an object, for the reasons you mention. I’m just pointing out that assuming a variable is a bool because it’s used in a Boolean context is a bit silly, especially in Python or other languages where any object can have a truthiness value, and where this is commonly utilised.It is not “assume” as in a conscious “this is probably a bool I will assume so” but more like a slip of attention by someone who is more used to the bool context of not. Is “not integer” or “not list” really that commonly used that it is even comparable to its usage in bool context?
It does if you are used to sane languages instead of the implicit conversion nonsense C and the “dynamic” languages are doing
Doesn’t matter what it implies. The entire purpose of programming is to make it so a human doesn’t have to go do something manually.
not x
tells me I need to go manually check what typex
is in Python.len(x) == 0
tells me that it’s being type-checked automatically