Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zen of Python 'Explicit is better than implicit'

Tags:

python

pep

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.

like image 414
sir_chocolate_soup Avatar asked Sep 25 '20 19:09

sir_chocolate_soup


People also ask

Is explicit better than implicit?

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.

What is the use of Zen of Python?

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.

Who wrote the Zen of Python?

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.

Which module prints the Zen of Python when imported?

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.


2 Answers

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:

  • Have you already verified whether a is a sequence?
  • Do you need to?
  • Do you mind if your if statement crashed on non-sequences?
  • Do you want to handle other falsy objects as if they were empty lists?

Remember that making the code look pretty takes second place to getting the job done correctly.

like image 181
Mad Physicist Avatar answered Oct 17 '22 14:10

Mad Physicist


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.

like image 20
ips1 38 Avatar answered Oct 17 '22 13:10

ips1 38