Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Style guidance from PEP 8
Lecture: Code layout and structure

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Next up, code layout. Let's see what PEP 8 has to say about it. The most important part about code layout that PEP 8 talks about is indentation.
0:09 So, imagine we have a method called some_method(), it says that you should indent four spaces, and PyCharm of course, as you've seen, knows PEP 8
0:18 and indents four spaces for us automatically, so: one, two, three, four, there we are.
0:23 And, we can write something so we could define a variable, hit Enter, like an "if" statement, so something like "if x is greater than two", enter;
0:31 Python does not like us to use tabs, PEP 8 recommends we use spaces and every indent is four spaces,
0:37 but it even looks like if I hit tab and delete tab and delete, hit tab, back and forth, it's actually four spaces
0:43 and PyCharm just treats blocks of four spaces like tabs for us so that it's easier for us to work with.
0:49 Next, PEP 8 also talks about spaces between methods and other statements, so for example here it says there should be two blank lines
0:58 between this method and anything else, somewhere down here two blank lines, two blank lines, I can go and fix this, but if I hit Command+Alt+L
1:05 it will actually fix all of those spaces for us. If we look within a method, PEP 8 says there should be either zero or one blank line
1:13 between any section of that method, and use the blank line sparingly but to indicate a logical grouping; so you can see maybe we wanted to define x
1:21 and later have this if statement, that would be fine, but if we go farther you can see "hey, PEP 8 warning, too many blank lines", put that back.
1:29 Last thing that I'll highlight is around classes, so if we have a class called AClass here, you can see it has three methods,
1:37 and just like functions, it should have two blank lines separated in it from other module or level things like the top of the file,
1:43 the method down here, and so on, but within the class, there should be one blank line between methods, not two.
1:50 So that's different than functions, when you are looking at methods within a class, it's a little more tightly grouped as you see here.
1:57 So let's look at that in a diagram, here we have our method, you can see the little dashes indicate spaces,
2:02 which have all of our symbols within our method indented four spaces, additionally four more spaces for loops and conditionals
2:08 and more and more as you nest those things, right, there should be two blank lines between methods, classes
2:14 and other symbols defined within your module, and finally, PEP 8 recommends that you don't have lines longer than 79 characters,
2:21 although there is a lot of debate around the value of exactly 79, but you know, it's a guidance, basically try to avoid having super long lines
2:30 but don't do so by changing your code structures so much that it becomes small,
2:34 so for example don't, like, create all your variables to just be one character
2:38 so that when you combine them in some expression they still fit on the line, you would be better off using the continuation,
2:44 but the idea of not having really long lines of code that's a good idea.


Talk Python's Mastodon Michael Kennedy's Mastodon