I’ve Used Python for Years and Just Learned These 3 Tricks Somehow

One bonus trick at the end

Kevin Lin
4 min readOct 14, 2022

1. Automatic f-string variable printing

Using { var = } in an f string means it’ll print the variable name and value

So we all know that f-strings are great because they let us format text inline with the variables we want to print out, e.g.

They’re also useful when debugging or logging, because we can write the variable names and their values together:

What I learned recently is that you don’t actually have to type out the variable name when you’re writing your f string. The following code prints the same thing as above (“debug mode”), but with less typing:

There is a small difference (just the quotes) but for the purpose of printing variable values it doesn’t really matter. Using this trick across your programming lifetime might save you an entire hour of your time so you’re welcome.

2. Inline assignment

:= is the inline assignment operator

Have you ever called a function thinking that you didn’t need to store the result in a variable, only to realize that you have to use that result again somewhere later in execution? Then, you cried as you added a line to your already-40-line-long function and went on with your life. Inline assignment operators were added in Python 3.8, and they help the situation a little bit.

Example:

normally gets refactored into

Makes sense, we’ve probably halved our run time.

Here’s what it looks like with the inline assignment though:

Basically, we use the := operator to assign the function result to expensive_result, which is used by the foo function, and the second line has “access” to expensive_result too! I will admit, the syntax looks a bit messy because the parentheses are usually required, but this is still something that is nice to know.

3. pdb.set_trace()

I discovered this through a Ruby analogy!

Before learning about pdb, my debugging toolkit consisted of printing variables, using IDE debuggers, and talking to a rubber duck. The built-in library pdb adds to this. Although it has lots of utilities I haven’t explored yet, the main feature for me is the set_trace() function.

You use it like so:

What this does is it sets a breakpoint at the line with set_trace(), and then opens a Python interpreter in your terminal when your code reaches that line. In that terminal, you can now examine your code in that freeze-frame. You can print out/inspect variables, create variables, call functions, and also step through code as you would with a debugger normally.

Maybe this method of debugging is common knowledge, but I’ve pretty much exclusively used IDE debuggers in the past, so I’m sure there are people like me who have never seen it before.

4. Bonus: Line Profiler

The last trick is a third party library, but it is useful enough to merit being written about.

line_profiler is a package that easily lets you figure how much time Python is spending on different parts of your code, which is helpful in figuring out which parts of your code need to be optimized.

For example, consider the following sample code:

Obviously, in real, useful code, we don’t know how long functions will take, but pretend that we weren’t sure what was taking up most of our time in our main function. We can use line_profiler now to figure it out.

First, we add a @profile decorator to the function of interest:

The IDE is unhappy, but now we just install line_profiler:

pip install line_profiler

and run it (the l option is for line-by-line, v is to view the results right away):

kernprof -lv main.py

and we see the output:

As you can see, we now know that long_function is the main time hog in our program.

That’s all the tricks I have for now — I learned all of these in the past month, and I’m always looking for ways to learn. Let me know if I made any mistakes or if there are any more tricks you would add to this list.

--

--

Kevin Lin
Kevin Lin

Written by Kevin Lin

Engineering Physicist and occasional content creator

Responses (1)