avatarYang Zhou

Summary

The web content outlines four Python functions—globals(), locals(), vars(), and dir()—that aid in reading and understanding Python code by providing information about variable names, values, and attributes within different scopes.

Abstract

The article titled "4 Functions That Make Reading Python Code Easier" discusses the challenges of reading large Python codebases and introduces four built-in functions to facilitate the process. globals() returns a dictionary of all global variables and their values, while locals() does the same for local variables within a function or class. The vars() function provides the __dict__ attribute of an object, revealing its namespace, and dir() lists all valid attributes and methods of an object, including special ones. The article emphasizes that these functions are particularly useful when exploring code in a Python console, making the reading process more efficient and Pythonic, as opposed to manual tracking or note-taking.

Opinions

  • The author suggests that reading code is more challenging than writing it, implying a need for tools to simplify code comprehension.
  • The use of globals() and locals() is recommended for quick insights into variable names and values, avoiding the need to manually sift through code.
  • The article posits that manipulating the dictionary returned by globals() can help isolate specific data of interest, enhancing code exploration.
  • It is noted that locals() will yield the same result as globals() when called in the global scope, highlighting the interchangeability of the two functions in this context.
  • The vars() function is presented as a direct interface to an object's __dict__, providing a straightforward way to inspect an object's namespace, but with the caveat that not all objects have a __dict__.
  • The dir() function is highlighted for its ability to list names in the current scope, with a note that it internally calls __dir__(), which can be customized to alter the output of dir().
  • The author's perspective is that these functions are essential for a Python developer to enjoy reading code, suggesting that they contribute to a more pleasant and efficient development experience.

4 Functions That Make Reading Python Code Easier

Your Name by Makoto Shinkai

Introduction

When a program becomes large, reading the code becomes a difficult job. Even some simple tasks will be hard. For example:

  • How to quick look all variable names and values for the current script?
  • How to check all variable names and values of a large function or class?
  • How to get a list of valid attributes of a specific object?

Of course, we can look up the code line after line and keep the names in mind or write them in papers. However, it’s not Pythonic at all.

It’s harder to read code than to write it. — Joel Spolsky

To make our lives easier, Python provides four useful built-in functions to help us display variable names and values of a specific scope conveniently. This post will dive into these four ways to make us enjoy reading code.

Function 1: globals()

As its name implies, the globals() function will display information of global scope.

For example, if we open a Python console and input globals(), a dict including all names and values of variables in global scope will be returned.

>>> globals()
{'__name__': '__main__', '__doc__': None, ...}

(Some example outputs of this post are abbreviated by ... to make them more neat and readable.)

If we add one variable:

>>> Master = "Yang"
>>> globals
{'__name__': '__main__', '__doc__': None, ... ,'Master': 'Yang'}

Since the globals() function just returns a dict. We can manipulate this dict to get some specific data we are interested in:

>>> [n for n in globals() if not n.startswith('__')]
['sys', 'Master']

As the above example shown, we use a list comprehension to get all the variable names without double leading underscores.

Function 2: locals()

After understanding the globals(), locals() function is just a piece of cake. As its name implies, it will return a dict including all local names and values.

By the way, if we call the locals() in global scope, the result is identical to globals().

>>> globals() is locals()
True

Function 3: vars()

The vars() function will return the __dict__, which is a dictionary used to store an object’s attributes. Its result is the same as calling the __dict__ directly.

It’s not necessarily that all objects have the __dict__, so we can only use the vars() method in parts of objects.

As the above example shown, an int type object doesn't contain a __dict__, so a TypeError will be raised if we use vars() on it.

Function 4: dir()

The dir() function helps us demonstrate a list of names in the corresponding scope.

In fact, the dir method calls __dir__() internally.

As shown above, if we change how the __dir__ works, the result of dir() will also be changed.

Conclusion

These four built-in functions are useful tools for us to display names and values. Their common usage scenario is being used in a Python console to explore a script or an object when we are reading programs.

Programming
Python
Python Programming
Software Development
Python3
Recommended from ReadMedium