I'm not sure if this is an opinionated question or if it is me misunderstanding what 'implicit' and 'explicit' really means in the context of Python.
a = []
# my understanding is that this is implicit
if not a:
print("list is empty")
# my understanding is that this is explicit
if len(a) == 0:
print("list is empty")
I'm trying to follow the Zen of Python rule, but I'm curious to know if this applies in this situation or if I am over-thinking it? Appreciate any guidance I can get.
Explicit is better than implicit It means that it is better to make the code more verbose and explicit. Doing this will make the code readable. Hiding code functionality might have repercussions as other programs might not be able to understand the code.
The Zen of Python is a collection of 19 "guiding principles" for writing computer programs that influence the design of the Python programming language. Software engineer Tim Peters wrote this set of principles and posted it on the Python mailing list in 1999.
The Zen of Python by Tim Peters are 20 guidelines for the design of the Python language. Your Python code doesn't necessarily have to follow these guidelines, but they're good to keep in mind.
As soon as we'd chosen "import this" I realized we just had to implement it. Python 2.2 was about to be released and I proposed that we turn off check-in notifications and sneak in a "this.py" module which when imported just printed the Zen of Python.
The two statements have very different semantics. Remember that Python is dynamically typed.
For the case where a = []
, both not a
and len(a) == 0
are equivalent. A valid alternative might be to check not len(a)
. In some cases, you may even want to check for both emptiness and listness by doing a == []
.
But a
can be anything. For example, a = None
. The check not a
is fine, and will return True
. But len(a) == 0
will not be fine at all. Instead you will get TypeError: object of type 'NoneType' has no len()
. This is a totally valid option, but the if
statements do very different things and you have to pick which one you want.
(Almost) everything has a __bool__
method in Python, but not everything has __len__
. You have to decide which one to use based on the situation. Things to consider are:
a
is a sequence?if
statement crashed on non-sequences?Remember that making the code look pretty takes second place to getting the job done correctly.
Though this thread has some time, I'd like to offer a perspective. In a dynamic language, my preference would be to always describe the expected type and objective of a variable in order to offer more purpose understanding. Then use the knowledge of the language to be succinct and increase readability where possible (in python, an empty list's boolean result is false). Thus the code:
lst_colours = []
if not lst_colours:
print("list is empty")
Even better to convey meaning is using a variable for very specific checks.
lst_colours = []
b_is_list_empty = not lst_colours
if b_is_list_empty:
print("list is empty")
Checking a list is empty would be a common thing to do several times in a code base. So even better such things in a separate file helper function library. Thus isolating common checks, and reducing code duplication.
lst_colours = []
if b_is_list_empty(lst_colours):
print("list is empty")
def b_is_list_empty (lst):
......
Most importantly, add meaning as much as possible, have an agreed company standard to chose how to tackle the simple things, like variable naming and implicit/explicit code choices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With