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