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:00 Now I want to show you some settings you can make to your Sublime Text setup that will make it easier for you to write PEP 8 compliant Python. So, PEP 8 is the most common Python style guide and it’s really widely used in the Python community.

00:14 I’m going to show you some Sublime Text settings you can change that will make it easier for you to actually write PEP 8 compliant code. It’s mostly concerned with getting the whitespace settings correct so that you don’t have to manage this pretty boring aspect yourself.

00:28 We’ll also set up things like the visual indicators for the maximum allowed line length, so that you can write beautiful PEP 8 compliant Python just like Guido wants you to. Let’s improve our whitespace handling first,

00:45 and the way we’re going to make these changes is by not putting them into your user settings, but actually by putting them into your Settings - Syntax Specific, in your syntax-specific settings, because that means they will only apply to Python code so you’re not going to clutter up… You know, when you’re writing a JavaScript file or a C file, it will have its own settings and it’s not going to get cluttered up with your Python settings.

01:09 So, you want to make sure you have a Python file open or at the very least select the Python syntax in the lower right, and then go to the syntax-specific settings.

01:20 And this should show up as python.sublime-settings, so this is the one that you want, all right? We’re going to make a couple of changes here. First of all, we’re going to change the tab setting, the "tab_size" setting, to the recommended 4.

01:36 We’re also going to tell Sublime Text to use space characters instead of tab characters. And then another important setting is the "trim_trailing_white_space_on_save".

01:52 This is a bit of a long one, ha. And by the way, most of the available options you can find here on the left, in your default preferences. So we could search here for trim_trailing_white_space and then also do this, where we just do a copy and paste.

02:12 In some cases, that might save you some typos. Again, you can go to "ensure_newline_at_eof_on_save" and set that to true. What that’s going to do is every time you save a file, it’s going to add that required newline at the end for PEP 8 compliance.

02:36 Again, let’s switch back to some of our code. I want to walk you through some of these things we just changed here. First of all, this setting, this change is going to guarantee that we’re getting spaces instead of tab characters when we indent.

02:51 Then also, if we have extra whitespace at the end of a line, that’s going to get removed on save. So let’s say I add a bunch of extra whitespace here and then save that, the editor’s going to get rid of that extra whitespace for us, which is really handy, right?

03:07 So did you see that? We’re adding a bunch of extra whitespace, hitting Control + S, and it gets dropped automatically. Another thing is that we’re getting this auto newline at the end.

03:18 So here, this is the last line, and now when I save this, it adds another newline character here at the end so we have this blank line here, which is kind of the recommendation for PEP 8. And that means, you know, our linter isn’t going to complain about these things and we don’t have to worry about that.

03:37 Now, another thing I want to show you is how you can get indicators that show you how long your lines should be if you’re following PEP 8. And again, we’re going to go back to our syntax-specific settings,

03:51 and we’re going to use the "rulers" feature again. So again now, this is going to be a list and I’m going to place the rulers at 72 and 79 characters, which is the PEP 8 recommendation for Python code.

04:04 And you can see here that this code actually follows PEP 8 pretty well, right? The first one here at 72 characters, that column that kind of shows you where your docstrings should go, and then the rest of your code shouldn’t go longer than 79 characters. And, you know, whatever you pick, I don’t want to be too prescriptive with this stuff, but I found that adhering to PEP 8 actually had a really great and positive effect on my own code and also on the team that I worked with at the time, because we were all using the same settings and it just stopped these arguments around like, “Oh,” you know, “That line is too long and I can’t scroll that when I’m reviewing your code,” and all these problems—they just go away and it just stops that argument.

04:48 So I find that really helps for writing better Python, essentially. There’s another useful feature I want to show you, and that is turning on word wrapping. You can do that, again, by going back to the syntax-specific settings—unless you want to put that into your user settings, in which case it would apply to any file type and not just Python files.

05:10 So I’m going to turn on "word_wrap", and then we also need to tell Sublime where to wrap, and I’m just setting that to 80 characters.

05:20 What that’ll do is once I get a line that’s longer than 80 characters, it’s going to get wrapped around and so it’s going to stay within that 80-characters limit.

05:32 And now you might be wondering, “Okay, why do I need that?” And the reason for why I like this setting is when you actually go to View and then Layout, you can add multiple columns in a single Sublime Text window.

05:44 What I like to do is often edit the test files and my regular files at the same time. And so, you know, when I get rid of the sidebar as well and I use an 80-character limit, this actually fits pretty well.

06:04 So another thing that I like to do is to actually hide the minimap, because I don’t really have a use for it. So now when you look at this, obviously, I’m using a larger font than I normally would, so you can better see that in the video, but when I put that to full screen—and this is a 1080 resolution—it’s actually quite nice!

06:26 And you could imagine you could have even more columns if you crank down the font size a bit, right? But

06:33 even with this larger font size, I can fit these two files here and then if I work with code that doesn’t follow PEP 8, it’s still going to be wrapped around the 80 column mark which guarantees that I don’t have to scroll horizontally.

06:50 You know, it’s bad enough that we have to scroll vertically, so I don’t want to have to scroll horizontally as well. All right, so let’s switch back to you the single file layout or single column layout so that we have a little bit more space here on the screen. But yeah, this is a setting that I find extremely helpful for writing PEP 8 compliant code because now, you know, you just get these little visual hints and in combination with a linter, it’s a pretty nice setup. I’m really happy with that, and I hope you enjoy it too!

You must own this product to join the conversation.