Return to Video

PEP8

  • 0:00 - 0:03
    Today, we'll do a brief overview of PyCharm
  • 0:03 - 0:05
    and the PEP8 style guide. Style guides are
  • 0:05 - 0:07
    important because they help you to provice
  • 0:07 - 0:11
    clean, consistent and easy to read code. If
  • 0:11 - 0:13
    you're working in a team and someone is
  • 0:13 - 0:15
    reading code written by different team
  • 0:15 - 0:17
    members, you want them to be able to
  • 0:17 - 0:19
    focus on the content of the code, rather
  • 0:19 - 0:21
    than differences in formatting. And if you
  • 0:21 - 0:23
    move from one company to another,
  • 0:23 - 0:25
    you can count on having to learn a new
  • 0:25 - 0:28
    style guide. For Python, the default style
  • 0:28 - 0:31
    guide is called PEP 8. You can find PEP 8
  • 0:31 - 0:33
    online. The easiest thing to do is search
  • 0:33 - 0:35
    for PEP 8. It should be the first result,
  • 0:35 - 0:37
    and you should familiarize yourself
  • 0:37 - 0:40
    to the standard. While we will be strictly
  • 0:40 - 0:42
    adhering to PEP 8 in this class, I want to
  • 0:42 - 0:44
    draw your attention to this section called
  • 0:44 - 0:46
    "A Foolish Consistency is the Hobgoblin
  • 0:46 - 0:50
    of Little Minds" and basically it says you
  • 0:50 - 0:53
    should use PEP 8 most of the time, but if
  • 0:53 - 0:54
    PEP 8 is getting in the way of the
  • 0:54 - 0:56
    readability of the code, then you have
  • 0:56 - 0:58
    permission to deviate. For me, this
  • 0:58 - 1:00
    happens a lot of times when I have long
  • 1:00 - 1:03
    lines that I need to break up. If I follow
  • 1:03 - 1:05
    the PEP 8 too strictly, I end up with
  • 1:05 - 1:07
    code that's unreadable. Now I've written
  • 1:07 - 1:09
    some code here in PyCharm that's just
  • 1:09 - 1:11
    full of PEP 8 violations and some warnings
  • 1:11 - 1:14
    as well. PyCharm is nice because it alerts
  • 1:14 - 1:16
    you to your PEP 8 violations real time as
  • 1:16 - 1:18
    you code. It does that in the source code
  • 1:18 - 1:20
    window on the right hand side using
  • 1:20 - 1:23
    these dashes. And if we hover over the
  • 1:23 - 1:25
    dashes, we can see the specific violations.
  • 1:25 - 1:28
    This one says "PEP 8: expected 2 blank
  • 1:28 - 1:30
    lines, found one". I can click on that dash
  • 1:30 - 1:33
    and it brings me to the right violation.
  • 1:33 - 1:35
    If I hover over the line of code, I
  • 1:35 - 1:37
    see the violation again. "PEP 8: expected
  • 1:37 - 1:40
    2 blank lines, found 1" and what that means
  • 1:40 - 1:42
    is there should always be 2 blank lines
  • 1:42 - 1:44
    before a class definition. So I'm
  • 1:44 - 1:48
    going to enter another blank line,
  • 1:48 - 1:51
    and my violation goes away. Now we'll
  • 1:51 - 1:53
    continue down fixing this code so you
  • 1:53 - 1:55
    can see some of the more common violations.
  • 1:55 - 1:57
    Usually we want spaces around operators
  • 1:57 - 1:59
    like equals signs. One exception is with
  • 1:59 - 2:02
    default values to parameters. Here, we
  • 2:02 - 2:04
    have a parameter "journey_length" that
  • 2:04 - 2:06
    has a default value of 7. In this case, I
  • 2:06 - 2:09
    want to take out the spaces around the
  • 2:09 - 2:11
    equals sign. Of course in the next line,
  • 2:11 - 2:13
    we have the opposite problem. It says
  • 2:13 - 2:15
    "PEP 8: missing white space around operator"
  • 2:15 - 2:17
    and that's because this is not a default
  • 2:17 - 2:21
    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.
Title:
PEP8
Video Language:
English
Duration:
05:17
VexWard edited English subtitles for PEP8
VexWard edited English subtitles for PEP8

English subtitles

Incomplete

Revisions