Beautiful Python Code Layout
In this lesson, you’ll learn how to improve the readability of your scripts using blank lines and line breaking. You’ll see the preferred way to add blank lines to classes and functions and how this helps break up code into logical sections.
PEP 8 suggests that lines should be limited to 79 characters but it is not always possible to keep statements below that. You’ll see how to handle this 79 character limit and the recommended ways of breaking up statements over multiple lines.
00:02 Now that you can use PEP 8 to name your variables, it’s time to focus on how your code is laid out. Here you’ll learn how PEP 8 recommends you use blank lines, and when to break to a new line.
00:14 Blank lines can be used to break up your code into pieces in much the same way paragraphs break up text into ideas. Thinking about it this way, you probably already have some intuition as how to use blank lines in your scripts. Even still, here are some guidelines to follow as you write Python.
00:31 For a top-level function or class, separate each with two blank lines.
01:01 Since these are likely pretty self-contained and separate from each other, this makes sense. You don’t want the reader to get confused with where one function ends and the next begins. When dealing with classes, feel free to separate each method call with a single blank line.
01:34 This shows that they’re both related, while still being separate from any other classes or functions. When you’re inside a function, it can be helpful to separate steps out with a blank line here and there.
01:44 If your code is broken into logical steps, this can greatly improve readability. Take a look at this function here to calculate variance. Right now it’s just a mass of different calls and operations, but it can be made a lot clearer just by adding a couple blank lines. By putting a space here, you can separate out this section of the function with this section of the function. And even better yet, put another blank line here and show that you will be returning the results from both of these steps. Overall, you want to break up your code with blank lines enough to help it be readable without stretching it out too much.
02:20 Too many blank lines can hurt readability by requiring excess scrolling to read through an idea. This brings us to the next aspect of code layout—maximum line lengths. Let me go ahead and delete this.
02:46 PEP 8 recommends that lines are limited to 79 characters, which helps if someone has multiple files open side by side. Sometimes, especially with certain libraries, it can be impossible to keep the line length to 79 characters or less, so PEP 8 has some options for that. You can use implied continuation if you’re working within parentheses, brackets, or curly braces.
03:33 This is called implied continuation because you don’t actually put anything that says that you’re going to move to another line. Python just knows that everything that’s within these parentheses or brackets or braces is meant to be on one line.
03:49 Sometimes, though, you need to break lines and you’re not within parentheses or brackets.
04:08 In this case, you can use a backslash to force Python to look to the next line. If you have the option to use implied continuation, you should do that instead, as that’s the preferred method.
04:22 One thing to consider when breaking lines is to make sure you can still follow the code as it goes to the new line.
04:42 In the case of binary operators, like a plus or minus sign, it reads a bit better to break before the operator. You can see that this is a little bit easier to read than something like this.
04:58 Technically, this is still PEP 8 compliant as long as the rest of the script is broken out this way, but since the other way is preferred, you should do this as it makes it much easier to stay consistent.
05:12 And that’s it! I hope you picked up some good tips on how to use blank lines and line breaking to improve the readability of your scripts. In the next video, we’ll cover indentation. Thanks for watching.
Become a Member to join the conversation.