Salvatore Lopiparo

Senior Software Engineer

Super Simple Python – Function and Variable Names

One of the best ways to keep your code clean and understandable is to use well defined function and variable names. Take, for example, the following bit of code:

1
2
3
4
5
6
7
def get_stuff(p, dirs=False):
    return_list = []
    for a, b, c in os.path.walk(p):
        return_list.extend(c)
        if dirs:
            return_list.extend(b)
    return return_list

It takes a lot of effort to figure out what is going on here. We can tell from the function name get_stuff that it’s used to get something, but what is it getting? The arguements p and dirs are very ambiguous. The p argument says nothing about what it is, and the dirs argument implies that it is a list of directories, but it’s actually a boolean! The a, b, and c variables have completely useless names. At least return_list tells us that it’s intended to be returned… though a good comment can do that much clearer!

Here’s the same function re-written with much more understandable function and variable names.

1
2
3
4
5
6
7
8
9
10
def get_file_names(path, include_directories=False):
    """
    Gets a list of all file names in the given path, including files in subdirectories.
    """
    list_of_file_names = []
    for root, dirs, file_names in os.path.walk(path):
        list_of_file_names.extend(file_names)
        if include_directories:
            list_of_file_names.extend(dirs)
    return list_of_file_names

For those people who are worried about the extra time they’re saving by using short names, think again! If you find a bug in the code or need to modify it in the future, trying to decipher the cryptic function will more than make up the difference in time. In addition, all modern IDEs (and even some text editors) have code completion, letting you only need to type a couple letters to fill out an entire variable name.

Super Simple Python #1 – Doing Math and Assigning Variables

Wait, wait! Don’t groan yet! It’s really simple math! Like, pluses and minuses and stuff! No long division, and no integrating differential equations, I promise! That’s a later chapter!

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

Open up the PyCharm Python Console (see SSP#0). You’ll notice that the lines you type on start with “>>>”. That’s normal. You’ll also notice that I’ll start things I want you to type with “>>>”. That’s also normal. You should not, however, type extra “>>>”. That’s not normal. The line after the “>>>” is the output line. That’s also also normal. You’ll also notice that I say also too much. That’s also not normal.

Now some math! Type:

1
2
>>> 1 + 1
2

Yay! Math! See? Easy stuff. Now try:

1
2
>>> 1 - 1
0

And it works like that with all the numbers, including negatives!

1
2
3
4
>>> 3 + 5 - 10
-2
>>> 111 * 7777
863247

Now a variable! A variable is a name for a piece of data, like a number.

1
>>> bananas = 3

We call that “assigning 3 to bananas.” You may have noticed that it didn’t have any output. That’s because the number 3 went “into” the variable bananas. Now if we type:

1
2
>>> bananas
3

There’s our 3!

We can also do:

1
2
3
>>> apples = 10 - 6
>>> apples
4

There, we subtracted 6 from 10, then assigned that (4) to apples! It will always completely figure out the right side of the = before assigning the value (in this case 10 - 6).

You can also do math on variables that contain numbers:

1
2
3
4
5
6
7
>>> apples + 10
14
>>> apples  # Notice here how apples did not change!
4
>>> apples = apples + 10  # Now it will change, because we assigned it again!
>>> apples
14

In addition to adding numbers to variables, you can add variables to other variables:

1
2
3
4
5
6
7
>>> apples + apples
28
>>> apples
14
>>> fruits = bananas + apples
>>>fruits
17

Whew! Math is done. That wasn’t so bad, was it?

Questions or comments? Let me know below!