Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

PEP 8 Python

00:01 Now I’m going to show you a few settings you can change to make it easier for you to write PEP 8 compliant Python with Sublime Text. PEP 8 is the most common Python style guide and is widely used in the Python community. In this lesson, I’m going to show you a couple of tweaks and improvements you can make so you can write concise and beautifully PEP 8 compliant code exactly like Guido wants us to. To start, you want to open the syntax-specific settings here.

00:26 The way this works is you want to make sure you have a Python file open, in that the syntax here is set to Python. And then when you go to Preferences and click on Syntax Specific, then this will open the Python-specific settings here.

00:40 And all the settings you make here will only be applied whenever you’re editing a Python file. This is the perfect space for us to put our Python-specific settings.

00:48 We’re going to start by making some changes to the whitespace handling so that your Python code can stay clean and consistent. So, first of all, we’re going to set the "tab_size" to 4, and this will ensure that a tab character corresponds to four spaces.

01:03 And we’re going to translate these tabs to spaces with this setting that is called "translate_tabs_to_spaces". And this will make sure that we’re following the PEP 8 recommendations of using four-space indentations and no tab characters in our Python code.

01:20 And then the next setting is "trim_trailing_white_space_on_save", and it does pretty much exactly what it says. This will remove any superfluous whitespace at the end of your lines—again, which is super helpful if you’re writing Python because this will make sure whenever you commit a file to Git or to another source control system, that you won’t have conflicts based on some extra whitespace that wouldn’t really matter anyway, so you might as well get rid of that.

01:46 And so you don’t have to manage this boring aspect yourself, your editor’s going to do it for you. So we’re going to turn that on as well. And then there’s one more PEP 8 related setting, and that’s called "ensure_newline_at_eof_on_save", and we’re also going to set that to true.

02:03 And now I’m going to show you what these settings do. So, the "tab_size" setting made sure that every time we indent a line of code, it gets indented by four space characters.

02:13 And that’s the recommendation of PEP 8, so it makes sense to follow this recommendation.

02:18 You can also change this setting by clicking on the lower right and then picking different settings, but generally a Tab Width of 4, and then checking that Indent Using Spaces option—that’s the one you want for Python code in general.

02:31 The next setting was the "trim_trailing_white_space_on_save" option, and what that will do is when you have extra whitespace at the end of your lines, or even here on a blank line, as soon as you save the file, then Sublime Text will automatically get rid of that whitespace for you so that it doesn’t clutter up your lines.

02:49 This extra whitespace can be a problem when you’re working with a system like Git for source control, because then these lines can lead to merge conflicts if you have several people working with the same file. It can lead to all kinds of ugliness, so it’s best to avoid that.

03:03 And a really easy way to do that is by just letting Sublime Text handle this aspect for you, and then you don’t have to worry about it. You just save your file and it gets rid of all the extra whitespace for you.

03:13 There’s one more setting here that’s important for PEP 8 compliance, and that is that Python files should end with a blank line. So with the "ensure_newline_at_eof_on_save" setting, this will be handled automatically by Sublime Text.

03:26 So now when the file ends without a newline and I save the file, then Sublime Text is going to add that newline character and we’re starting on a blank line here, which is the recommendation that PEP 8 makes and it has to do with how POSIX tools handle and process text files. And again, it’s a super boring aspect that you don’t want to be concerned with every day.

03:44 You just want to make sure that your Sublime Text handles these settings correctly, and then you never have to worry about it again. Another setting that is really handy for writing PEP 8 compliant code is the rulers feature you’ve seen before. So we’re going to turn that on now. Again, we’re going to go back to your Python-specific settings, then we’re going to add this "rulers" option with a list here, and we’re going to place these rulers at 72 and 79 characters, which is the recommendation made by PEP 8. And you’ll see here why. So, first of all, the 72 characters limit—that’s meant for documentation strings.

04:19 The idea is that you would limit those to 72 characters so that they print nicely in a terminal, and this is just a limit on the line length here for readability.

04:28 And so if you stay within that limit, that will make sure your code is PEP 8 compliant. And the 79 character limit applies for regular lines of Python code, where you want to make sure they don’t go past the 79 character limit.

04:39 Of course, with all of these line length limits, this is a personal decision again, and it’s a decision that needs to be made on a project basis, right? Depending on the people you work with, you might want to have a longer line length setting or a shorter one, but it really helps to have some kind of agreement there because it just makes it a lot easier to keep the code nice and consistent. So personally, I like adhering to the PEP 8 limit of 79 characters, because it makes sure that you can display code nicely side by side. And I’m going to show you how that works. In Sublime Text, when you go to View > Layout, you can select between different column- and row-based or grid-based layouts here, and I want to show you the two-column layout now that’s really helpful for working on the test code and the implementation code at the same time. So let’s turn that on. And then you can see here, we get these two columns, and I’m going to open the test for that file at the same time.

05:30 And now I hope you can see why it’s a good idea to limit the number of characters in your line, because if the lines are extremely long, then it becomes really hard to fit them here side by side.

05:40 There are two more tweaks we can make here. The first one is to get rid of the minimap, which is kind of nice to have, but I don’t really have to have it around, so I usually turn it off so I get a little bit more space and less cluttering.

05:53 And then we can also disable the sidebar, and you can do that by pressing down the Control key, and then hitting K + B, and that removes the sidebar. And with the same shortcut Control + K + B, then you can bring it back as well. It’s really easy to toggle between that.

06:08 And now you can see here that we’re having these two files side by side and I can browse around here, work on my code, and it doesn’t extend past the line limit, so I don’t have to scroll vertically here and I can fit these two files next to each other comfortably, even at a larger font size, which is awesome.

06:25 But of course, not all Python code that you’re going to be working with will adhere to the 80 character PEP 8 limit. So, what happens when you have extra characters here?

06:35 As I extend the line, you can see here that I’m getting this scroll bar and now to see everything, I have to scroll here horizontally, which is not ideal.

06:44 Luckily, there’s a way we can improve that situation, and that has to do with the "word_wrap" setting. So again, you want to go back to your syntax-specific settings for Python code, and we’re going to enable the "word_wrap" feature now.

06:57 You want to set the "word_wrap" option to true, and then also set the "wrap_width" option to 80 characters.

07:04 We’re going to save that, and when I switch back, you can see here that the super long line actually gets wrapped around, and this is indicated here in the gutter area.

07:13 Here we can see that line 94 actually takes up two lines as it’s being rendered, even though the original text file only had a single line for all of this text, or all of this code here.

07:26 The "word_wrap" setting is really convenient because it ensures that we never have to scroll horizontally, and only vertically. And so this makes working with code a lot easier because you can’t accidentally scroll around and you don’t have to scroll around to see all of the code.

07:40 That’s why this is really helpful when you’re working with two Python files side by side and you want to make sure you can see all of the code at the same time. To recap, I showed you a couple of changes you can make to your Python-specific settings in Sublime Text, and they will allow you to write beautiful and PEP 8 compliant Python without having to worry about the minutia of “Oh, do I need to remove these trailing whitespaces?” “Oh, do I need to ensure there’s a newline at the end of a file?” You just set these once and then you can forget about them and Sublime Text will do the right thing for you.

You must own this product to join the conversation.