1
00:00:04,700 --> 00:00:08,090
In this video, we're going to take a
break from writing code and look at

2
00:00:08,090 --> 00:00:12,440
style of our code. The first place to
start when talking about style and

3
00:00:12,440 --> 00:00:22,490
Python code is PEP 8. So I'm going to
search for PEP 8, and the first result

4
00:00:22,490 --> 00:00:25,880
should be the page of python.org and
you can see that is the case for me,

5
00:00:25,880 --> 00:00:31,339
PEP 8 style guide. So I'm going to open
that up, and you can see that, in fact,

6
00:00:31,340 --> 00:00:39,580
they have got a PEP 8 style guide for Python code.
So a PEP is a Python Enhancement Proposal.

7
00:00:39,580 --> 00:00:42,820
All changes in new features to
the python language start with a

8
00:00:42,829 --> 00:00:47,840
proposal. The proposal is reviewed,
discussed, improved if necessary, and then

9
00:00:47,840 --> 00:00:54,109
either accepted or rejected. Until 2018,
Guido van Rossum made the final decision

10
00:00:54,109 --> 00:00:58,280
about whether to implement a PEP or not.
Since then, the decision is taken by a

11
00:00:58,280 --> 00:01:03,710
group of people. Now you can see who they are
by looking at PEP 0013, so let's just open

12
00:01:03,710 --> 00:01:10,520
another tab here. I'm going to paste the
link in the resources section, as

13
00:01:10,520 --> 00:01:17,509
per normal. So as you can see, any
changes to python are made via a PEP,

14
00:01:17,509 --> 00:01:22,460
and that includes administrative changes, as
well as technical ones. Scrolling down,

15
00:01:22,460 --> 00:01:25,980
we've got this steering committee,
current steering council, I should say.

16
00:01:25,980 --> 00:01:29,659
You may recognize three of the names from the
PEP 8 page we were just looking at.

17
00:01:29,659 --> 00:01:33,110
I'll leave you to read this PEP 0013
if you're interested, and

18
00:01:33,110 --> 00:01:38,540
I'll switch back to the PEP 8 page now.
Alright, so PEP 8 was written by Guido

19
00:01:38,540 --> 00:01:43,120
van Rossum, Barry Warsaw and Nick
Coghlan, way back in 2001.

20
00:01:43,120 --> 00:01:46,939
They've all been involved with Python
for a long time. Guido wrote the first

21
00:01:46,939 --> 00:01:51,799
version and a few subsequent ones,
starting back in 1989. Now I don't

22
00:01:51,799 --> 00:01:55,670
suggest you read all of this document
right away. Most of the code examples

23
00:01:55,670 --> 00:01:59,780
won't make much sense, and you'll find it
confusing. I'm going to go through some

24
00:01:59,780 --> 00:02:03,049
sections that will make sense.
After that, check back in here as you

25
00:02:03,049 --> 00:02:07,460
learn more about Python. So the last two
paragraphs of the introduction are worth

26
00:02:07,460 --> 00:02:12,830
reading. This style guide evolves over time.
Python 3 has some features that weren't

27
00:02:12,830 --> 00:02:18,350
available in Python 2, and the guide has
had to change to deal with new features.

28
00:02:18,350 --> 00:02:22,100
The last paragraph is important. If you
work on a project that has its own style

29
00:02:22,100 --> 00:02:26,720
guide, then that should take precedence
over this one. For example, google have

30
00:02:26,720 --> 00:02:31,040
their own Python style guide. If you end
up working for Google, then your code

31
00:02:31,040 --> 00:02:35,750
should conform to their guide.
Alright, so moving down, read through the

32
00:02:35,750 --> 00:02:41,390
next section - A Foolish Consistency is the
Hobgoblin of Little Minds. So it contains

33
00:02:41,390 --> 00:02:45,710
some useful advice on when you should
not follow a style guide. Point number

34
00:02:45,710 --> 00:02:50,960
three is important; Don't change code to
make it conform to a certain style, when

35
00:02:50,960 --> 00:02:55,160
there's no other reason to be changing
that code. A simple typing error could

36
00:02:55,160 --> 00:02:58,760
break the code and cause all sorts of
problems. Unless you're prepared to

37
00:02:58,760 --> 00:03:02,930
explain why you broke a major program,
such as Google search engine, just

38
00:03:02,930 --> 00:03:06,590
because you didn't like the name of a
variable, then don't do it. If you're

39
00:03:06,590 --> 00:03:10,310
going to be changing the program anyway,
and it's going to be tested afterwards,

40
00:03:10,310 --> 00:03:14,900
then you could take the opportunity to
clean it up. I'm going to skip, as we go

41
00:03:14,900 --> 00:03:19,460
down, most of this Code Lay-out section.
That's not because it isn't important -

42
00:03:19,460 --> 00:03:24,040
it is - but it contains a lot of Python
statements that we haven't seen yet.

43
00:03:24,040 --> 00:03:31,240
So moving down to the tabs and spaces
section, the recommendation there is to

44
00:03:31,250 --> 00:03:35,720
use spaces to indent your code. That's
important for programmers from other

45
00:03:35,720 --> 00:03:40,820
languages. If you're used to using tabs,
then you should use spaces in Python.

46
00:03:40,820 --> 00:03:45,350
If you don't, and your IDE said to use tabs,
then you'll get problems on Python

47
00:03:45,350 --> 00:03:50,390
3 when you open your code, written to use
spaces. So next is the Maximum Line

48
00:03:50,390 --> 00:03:55,490
Length. Now pay attention to this section.
These suggested line lengths are intended

49
00:03:55,490 --> 00:03:59,060
for when you edit Python code in a
terminal, which is typically 80

50
00:03:59,060 --> 00:04:05,000
characters wide. As you'll see soon, the
JetBrains IDEs suggest 120 characters,

51
00:04:05,000 --> 00:04:10,460
rather than 79. Google suggest a maximum
line length of 80 characters, with a few

52
00:04:10,460 --> 00:04:15,500
exceptions. Stick to 72 characters for
comments and docstrings, because it's

53
00:04:15,500 --> 00:04:20,269
very hard for tools to format them. For
this course, I'll generally stick to less

54
00:04:20,269 --> 00:04:23,900
than 80 characters for the line length.
That makes it easier for you to see the

55
00:04:23,900 --> 00:04:28,580
code in the videos, and allows the code
to display correctly in an e-book.

56
00:04:28,580 --> 00:04:37,500
Alright, so moving on. I'm going to skip
to the Naming Conventions section.

57
00:04:37,980 --> 00:04:42,260
So the Naming Conventions section starts
with an admission, that a lot of the code

58
00:04:42,269 --> 00:04:47,489
in the Python libraries are quote, a bit
of a mess, unquote, as far as sticking to

59
00:04:47,489 --> 00:04:52,559
the naming conventions. This style guide
was written in 2001, and Python had been

60
00:04:52,559 --> 00:04:57,179
used for 10 years before this guide was
written. When you come to use code from

61
00:04:57,180 --> 00:05:00,899
the standard library, which we'll be
doing in later videos in the course, you'll

62
00:05:00,900 --> 00:05:05,020
find that many things don't conform to
this guide. Don't worry about it.

63
00:05:05,020 --> 00:05:08,600
The developers have read point three above,
which I talked about earlier in the

64
00:05:08,609 --> 00:05:13,109
video, and not changed the style just for
the sake of it. We've used variables in

65
00:05:13,109 --> 00:05:17,640
our code so far, so let's see what the
guide has to say about variable names,

66
00:05:17,640 --> 00:05:20,939
and that's specifically, as we go down a little
bit further now, in the Function and

67
00:05:20,939 --> 00:05:29,459
Variable Names section. Variable names
should follow the same convention as

68
00:05:29,459 --> 00:05:34,349
function names, which means they should
be in lowercase, with underscores used to

69
00:05:34,349 --> 00:05:39,539
separate words if necessary. Mixed case,
which is the convention in languages

70
00:05:39,539 --> 00:05:44,699
like Java and C++, should be avoided. Okay,
so bookmark this document in your

71
00:05:44,700 --> 00:05:49,840
browser and refer back to it as we cover
more Python statements in the course.

72
00:05:49,840 --> 00:05:53,669
Alright, so back in IntelliJ, you can't
actually see it at the moment because

73
00:05:53,669 --> 00:05:58,940
of how compressed I've got my screen, so
that you can see the videos in a good font,

74
00:05:58,940 --> 00:06:03,719
but there's a vertical guide in
IntelliJ set at 120 characters. Now what

75
00:06:03,719 --> 00:06:09,149
I can actually do, is I'm going to
temporarily just put some long code in

76
00:06:09,149 --> 00:06:13,019
here, just so we can see it. I'm gonna put
a hash there for a comment and I'm just

77
00:06:13,019 --> 00:06:18,779
going to paste some code, so keep pasting,
and there's the vertical guide that I

78
00:06:18,779 --> 00:06:22,019
was talking about. Now obviously, you can
see we've got some warnings, now that

79
00:06:22,019 --> 00:06:25,109
I've done that. We've exceeded the 120
characters. So this is basically the

80
00:06:25,109 --> 00:06:30,179
vertical guide I'm talking about, currently
set to 120 characters. Alright, so I'm just

81
00:06:30,179 --> 00:06:36,299
going to undo that change, or rather
delete that line. Now if you want to

82
00:06:36,299 --> 00:06:41,369
conform to the Python style guide, 120
characters is too long. The JetBrains

83
00:06:41,369 --> 00:06:44,789
IDEs allow you to change the
position of that guide. You can even set

84
00:06:44,789 --> 00:06:48,059
more than one, which is useful. So to do
that, let's have a look at how to do that.

85
00:06:48,059 --> 00:06:51,110
We're gonna to go into File Settings.

86
00:06:51,330 --> 00:06:54,900
Then you need to click on Editor and then Code
Style and I'm already there, you can see.

87
00:06:54,900 --> 00:06:58,440
Now, you can see under visual guides, I've
got nothing in there at the moment,

88
00:06:58,440 --> 00:07:03,000
although it does say hard rap at 120,
above that. Now most modern IDEs will

89
00:07:03,000 --> 00:07:07,760
have a similar setting. I want comments
and docstrings to be no longer than 72

90
00:07:07,760 --> 00:07:12,680
characters, and code to stop at 80
characters. So to achieve that, and the

91
00:07:12,690 --> 00:07:19,050
visual guides down here, I'm going to
enter 72 comma 80 in this box, and click

92
00:07:19,050 --> 00:07:24,360
on OK. And now you can see, we've got
these fake vertical guides to show us

93
00:07:24,360 --> 00:07:28,379
where 72 and 80 characters are, and now I
can see it clearly on my screen,

94
00:07:28,379 --> 00:07:32,879
so that's good. Now there's still the darker
guide at 120 characters, which we can't

95
00:07:32,879 --> 00:07:36,780
see, unless I go back and do what I did
earlier, and that's set by that hard

96
00:07:36,780 --> 00:07:40,050
wrap at setting, which you saw at the top,
and you can go back into the settings to

97
00:07:40,050 --> 00:07:43,319
change that if you want. But I'll leave
that, as is. Alright, so what I want to do,

98
00:07:43,319 --> 00:07:47,039
though, is finish this video with a
challenge. You need to open up the escape

99
00:07:47,039 --> 00:07:51,120
char.py from the previous section,
and I've actually copied it into this

100
00:07:51,120 --> 00:07:57,040
project, so open that up.

101
00:07:57,040 --> 00:08:00,860
Now this doesn't conform to the PEP 8 style guide.

102
00:08:00,860 --> 00:08:04,080
I don't want you to change the code at the
moment. That's because I'm going to show

103
00:08:04,080 --> 00:08:08,490
you a safer way of changing it in the
next video. Your challenge is simply to

104
00:08:08,490 --> 00:08:13,650
identify the things that don't conform
to the PEP 8 style guide. There are three

105
00:08:13,650 --> 00:08:18,389
problems with this code, which will need
changing on 6 lines, but again, don't

106
00:08:18,389 --> 00:08:22,500
change them yet. Just work out what these
things are - the things that don't conform

107
00:08:22,500 --> 00:08:27,090
to the PEP 8 style guide. I'll go over
them, and show you a cool way to fix them,

108
00:08:27,090 --> 00:08:30,289
in the next video.

