-
Today, we'll do a brief overview of PyCharm
-
and the PEP8 style guide. Style guides are
-
important because they help you to provice
-
clean, consistent and easy to read code. If
-
you're working in a team and someone is
-
reading code written by different team
-
members, you want them to be able to
-
focus on the content of the code, rather
-
than differences in formatting. And if you
-
move from one company to another,
-
you can count on having to learn a new
-
style guide. For Python, the default style
-
guide is called PEP 8. You can find PEP 8
-
online. The easiest thing to do is search
-
for PEP 8. It should be the first result,
-
and you should familiarize yourself
-
to the standard. While we will be strictly
-
adhering to PEP 8 in this class, I want to
-
draw your attention to this section called
-
"A Foolish Consistency is the Hobgoblin
-
of Little Minds" and basically it says you
-
should use PEP 8 most of the time, but if
-
PEP 8 is getting in the way of the
-
readability of the code, then you have
-
permission to deviate. For me, this
-
happens a lot of times when I have long
-
lines that I need to break up. If I follow
-
the PEP 8 too strictly, I end up with
-
code that's unreadable. Now I've written
-
some code here in PyCharm that's just
-
full of PEP 8 violations and some warnings
-
as well. PyCharm is nice because it alerts
-
you to your PEP 8 violations real time as
-
you code. It does that in the source code
-
window on the right hand side using
-
these dashes. And if we hover over the
-
dashes, we can see the specific violations.
-
This one says "PEP 8: expected 2 blank
-
lines, found one". I can click on that dash
-
and it brings me to the right violation.
-
If I hover over the line of code, I
-
see the violation again. "PEP 8: expected
-
2 blank lines, found 1" and what that means
-
is there should always be 2 blank lines
-
before a class definition. So I'm
-
going to enter another blank line,
-
and my violation goes away. Now we'll
-
continue down fixing this code so you
-
can see some of the more common violations.
-
Usually we want spaces around operators
-
like equals signs. One exception is with
-
default values to parameters. Here, we
-
have a parameter "journey_length" that
-
has a default value of 7. In this case, I
-
want to take out the spaces around the
-
equals sign. Of course in the next line,
-
we have the opposite problem. It says
-
"PEP 8: missing white space around operator"
-
and that's because this is not a default
-
value. This is an assignment inside the
-
Not Synced
method, so we want to add the white
-
Not Synced
space around the equals sign. The we
-
Not Synced
have a comment here, on European Swallow,
-
Not Synced
and inline comments need a space after the
-
Not Synced
hashtag to make them easier to read. They
-
Not Synced
also need 2 spaces before the hashtag.
-
Not Synced
You can see as we go along, the errors
-
Not Synced
are disappearing.
-
Not Synced
Inside a class, we should have one blank
-
Not Synced
line in between each method.
-
Not Synced
So we have to add a blank line before
-
Not Synced
this method "get_velocity".
-
Not Synced
The next issue is actually not a PEP 8
-
Not Synced
value, it's a warning, and you can see
-
Not Synced
the difference because it doesn't say
-
Not Synced
PEP8. The PEP8 violations are prepended
-
Not Synced
with PEP8. The warning is that we created
-
Not Synced
a new instance variable "velocity_gotten"
-
Not Synced
inside this method. Generally, instance
-
Not Synced
variables are used in several places inside
-
Not Synced
our code, so we'd like to initialize them
-
Not Synced
inside the unit method.
-
Not Synced
So what we'll do is we'll just add
-
Not Synced
"self.velocirt_gotten = " and we could
-
Not Synced
choose a default value. Probably
-
Not Synced
"false makes sense in this context,
-
Not Synced
or you might choose "none".
-
Not Synced
Push enter, and then that warning goes away.
-
Not Synced
Whenever we're comparing an object
-
Not Synced
to a built in type like None type,
-
Not Synced
we don't use equal to (=) or not equal to (!=),
-
Not Synced
we use "is" or "is not". So we're going to
-
Not Synced
replace (!=) with "is not". If you've been
-
Not Synced
coding in other languages, you might be
-
Not Synced
in the habit of adding semicolons to the
-
Not Synced
end of each each line. Python will accept
-
Not Synced
that, but it's not the standard, so we
-
Not Synced
want to get rid of that semicolon.
-
Not Synced
Again, we need a space between each
-
Not Synced
method. We almost always want a
-
Not Synced
single space after every comma,
-
Not Synced
and then after the end of the last line,
-
Not Synced
we should have a blank line.
-
Not Synced
Sometimes it will automatically indent, so
-
Not Synced
we'll just backspace, and let's go back and
-
Not Synced
see what's left. This is an interesting comment.
-
Not Synced
It says "method 'convert_to_mph' may be 'static'".
-
Not Synced
So, PyCharm noticed that we haven't
-
Not Synced
actually used any instance variables
-
Not Synced
inside this method, and it's letting
-
Not Synced
us know that we probably should be
-
Not Synced
coding this as a static method.
-
Not Synced
So we'll add the decorator, "@staticmethod"
-
Not Synced
static methods don't take a self parameter
-
Not Synced
and that warning is cleared.
-
Not Synced
Just two more issues here. First, we have
-
Not Synced
our class definition with parenthases.
-
Not Synced
Since we're not specifying any
-
Not Synced
class to inherit from, we can get rid of
-
Not Synced
the parentheses. And the final note
-
Not Synced
is that we haven't used our paramater
-
Not Synced
journey length anywhere in our code,
-
Not Synced
so we should either remove journey length
-
Not Synced
as a parameter, or perhaps, add an
-
Not Synced
assignment inside in it.
-
Not Synced
And now our code is clean, and we get
-
Not Synced
a satisfying little green check mark
-
Not Synced
up at the top.