< Return to Video

Python comments vs. docstrings: What, how, and why

  • 0:00 - 0:02
    REUVEN LERNER: Hey there, Python
    trainer Reuven Lerner here.
  • 0:02 - 0:05
    And I got a question
    from Roger, a subscriber
  • 0:05 - 0:08
    to my Better Developers
    mailing list, and he asks,
  • 0:08 - 0:11
    what's the difference between
    comments and docstrings?
  • 0:11 - 0:14
    What are the similarities
    and differences between them?
  • 0:14 - 0:16
    So let's talk about
    this a little bit.
  • 0:16 - 0:17
    Let's talk first about comments.
  • 0:17 - 0:20
    And we'll talk about docstrings,
    and then we'll compare them.
  • 0:20 - 0:24
    So if I write Python code, x
    equals 100, def hello name,
  • 0:24 - 0:26
    return--
  • 0:26 - 0:28
    this is hello name.
  • 0:28 - 0:31
    In all these cases,
    what's happening?
  • 0:31 - 0:33
    Well, I want the
    code to execute.
  • 0:33 - 0:36
    The code is meant for
    Python to actually do
  • 0:36 - 0:38
    something-- in the first
    case to define a variable x
  • 0:38 - 0:40
    and assign it 100,
    and the second case
  • 0:40 - 0:42
    to define a function.
  • 0:42 - 0:45
    And here I can then
    say hello world.
  • 0:45 - 0:48
    And now I want it to
    actually run that function.
  • 0:48 - 0:51
    In all these cases,
    Python takes whatever I
  • 0:51 - 0:53
    wrote, turns it into bytecodes.
  • 0:53 - 0:56
    It actually compiles my code
    into internal bytecodes,
  • 0:56 - 0:58
    and then it runs
    those bytecodes.
  • 0:58 - 1:00
    In the case of the function,
    it holds on to those bytecodes
  • 1:00 - 1:01
    later on.
  • 1:01 - 1:04
    Now it is absolutely, positively
    true that when you write code,
  • 1:04 - 1:07
    you should be thinking about the
    people who will be reading it
  • 1:07 - 1:09
    later on and
    maintaining it later on
  • 1:09 - 1:11
    because it's much, much
    easier to write code
  • 1:11 - 1:12
    than to maintain code.
  • 1:12 - 1:15
    So that readability--
    very important.
  • 1:15 - 1:18
    So use long variable names,
    use long function names.
  • 1:18 - 1:22
    But sometimes we need to give
    a little bit of an assistance.
  • 1:22 - 1:25
    It's not enough to
    just write clear code.
  • 1:25 - 1:27
    We sometimes need to tell
    people what we're doing
  • 1:27 - 1:30
    and, more importantly,
    why we're doing it.
  • 1:30 - 1:31
    And for that, we have comments.
  • 1:31 - 1:32
    So I can write a comment.
  • 1:32 - 1:35
    And in Python, a
    comment is hashmark
  • 1:35 - 1:36
    until the end of the line.
  • 1:36 - 1:39
    So this is a friendly function.
  • 1:39 - 1:40
    All right.
  • 1:40 - 1:43
    That's a very, very
    useless comment.
  • 1:43 - 1:43
    Why?
  • 1:43 - 1:45
    It doesn't give
    us any information
  • 1:45 - 1:48
    we could use about in order
    to maintain the function.
  • 1:48 - 1:49
    But it is a comment.
  • 1:49 - 1:50
    It's legitimately a comment.
  • 1:50 - 1:52
    And how does Python
    see a comment?
  • 1:52 - 1:53
    That's the thing.
  • 1:53 - 1:53
    Python doesn't.
  • 1:53 - 1:56
    It completely and
    utterly ignores them.
  • 1:56 - 1:58
    When we turn our
    functions into bytecodes,
  • 1:58 - 2:00
    the comments are thrown away.
  • 2:00 - 2:02
    They are only for
    the maintainers
  • 2:02 - 2:08
    and to make our code easier to
    understand, easier to maintain.
  • 2:08 - 2:10
    So what should you be
    writing in your comments,
  • 2:10 - 2:12
    and who are your
    comments aimed for?
  • 2:12 - 2:14
    Your comments are
    aimed for the people,
  • 2:14 - 2:15
    aimed at the people
    who are going
  • 2:15 - 2:17
    to be maintaining the function.
  • 2:17 - 2:20
    In this case, maintaining your
    module, maintain your code.
  • 2:20 - 2:23
    It's like the-- not the
    owner's manual for a car.
  • 2:23 - 2:24
    Rather, it's like
    the manual that
  • 2:24 - 2:27
    is for the people who will be
    fixing your car, who really need
  • 2:27 - 2:28
    to understand the internals.
  • 2:28 - 2:31
    So what exactly-- what would
    I do here, for example?
  • 2:31 - 2:34
    Well, maybe I would explain
    why I'm doing something.
  • 2:34 - 2:38
    Don't say something like return
    a string with the user's name.
  • 2:38 - 2:40
    That is really, really useless.
  • 2:40 - 2:43
    That's not going to
    help anyone at all.
  • 2:43 - 2:45
    We can already see that
    we've got an f string.
  • 2:45 - 2:46
    We already see that
    we've got the name there.
  • 2:46 - 2:49
    But we can say here
    something like return
  • 2:49 - 2:54
    a string instead of printing
    it for greater flexibility.
  • 2:54 - 2:56
    Now, that's still not
    a fantastic comment,
  • 2:56 - 2:59
    but at least it gives an insight
    into the thinking what we're
  • 2:59 - 3:01
    planning to do, thinking to do.
  • 3:01 - 3:03
    And so it gives some better
    sense of why we wrote it
  • 3:03 - 3:04
    the way we did.
  • 3:04 - 3:07
    And of course, there are
    lots of places in code,
  • 3:07 - 3:09
    especially older code, where
    you'll see things like,
  • 3:09 - 3:15
    don't touch this unless
    you know what you're doing,
  • 3:15 - 3:18
    or something like,
    here be dragons.
  • 3:18 - 3:21
    You don't want to do that,
    but people do it all the time.
  • 3:21 - 3:24
    OK, so that's comments.
  • 3:24 - 3:26
    And you should comment,
    I think, as little as
  • 3:26 - 3:30
    possible because I found that
    people use comments as a crutch.
  • 3:30 - 3:32
    I found that when you use
    comments in your code,
  • 3:32 - 3:35
    you're less likely to use
    good variable, good function
  • 3:35 - 3:38
    names, good other things
    to make your code readable.
  • 3:38 - 3:39
    But should you use comments?
  • 3:39 - 3:40
    Yeah.
  • 3:40 - 3:43
    No comments is, I think, a
    little extreme and unnecessary.
  • 3:43 - 3:44
    So definitely include comments.
  • 3:44 - 3:46
    Now, some people
    complain that you
  • 3:46 - 3:48
    can't have comments that
    stretch over one line, more
  • 3:48 - 3:49
    than one line.
  • 3:49 - 3:56
    If I have here comment 1,
    comment 2, and comment 3--
  • 3:56 - 3:58
    yeah, well, that's
    annoying, but that's just
  • 3:58 - 4:00
    the way it is-- you
    could, in theory, use
  • 4:00 - 4:03
    a triple-quoted string to
    go across several lines.
  • 4:03 - 4:06
    I really dislike that, but I
    know that many people do it.
  • 4:06 - 4:08
    OK, so those are comments.
  • 4:08 - 4:12
    So comment only on why not what.
  • 4:12 - 4:15
    Comment less rather
    than more in order
  • 4:15 - 4:18
    to force yourself to
    write clearer code,
  • 4:18 - 4:20
    and write the comments
    that are aimed
  • 4:20 - 4:22
    for the maintainers of the code.
  • 4:22 - 4:24
    So what's a docstring then?
  • 4:24 - 4:26
    Well, let's take
    our function here.
  • 4:26 - 4:26
    Hello.
  • 4:26 - 4:29
    And if I define my
    function-- and I'm
  • 4:29 - 4:30
    not quite sure how to use it.
  • 4:30 - 4:32
    So I'm going to say
    here help on hello.
  • 4:32 - 4:33
    And I hear that--
  • 4:33 - 4:35
    I find out from
    running help on it.
  • 4:35 - 4:38
    It's a function, and
    it takes one argument.
  • 4:38 - 4:39
    And the parameter
    is called name,
  • 4:39 - 4:41
    but I don't know
    much else about it.
  • 4:41 - 4:43
    So this is where a
    docstring comes in.
  • 4:43 - 4:46
    A docstring is a
    documentation string.
  • 4:46 - 4:49
    It is only there
    if you define it.
  • 4:49 - 4:50
    So you actually need to do this.
  • 4:50 - 4:52
    It's traditionally a
    triple-quoted string,
  • 4:52 - 4:55
    and it is aimed at the people
    who will be using your function,
  • 4:55 - 4:57
    not the people who will be
    maintaining your function.
  • 4:57 - 4:59
    Big difference.
  • 4:59 - 5:00
    Imagine you, the
    owner of your car,
  • 5:00 - 5:03
    versus the garage that
    fixes your car-- not
  • 5:03 - 5:04
    the same audience.
  • 5:04 - 5:07
    So the comments can go into
    deeper technical detail.
  • 5:07 - 5:11
    The docstring really should be,
    how do you use this function?
  • 5:11 - 5:12
    What does it do?
  • 5:12 - 5:16
    So returns a string with
    a friendly greeting.
  • 5:16 - 5:19
    Now back when I was taking
    software engineering
  • 5:19 - 5:22
    in my university days,
    a long, long time ago,
  • 5:22 - 5:26
    our professor told us that
    documentation for the user
  • 5:26 - 5:28
    API docs should
    include three things--
  • 5:28 - 5:31
    what does it expect, expects--
  • 5:31 - 5:33
    or I should say, yeah, expects--
  • 5:33 - 5:36
    and modifies and returns.
  • 5:36 - 5:39
    And it turns out
    this combination
  • 5:39 - 5:41
    is great to have in
    the documentation.
  • 5:41 - 5:42
    So what does this expect?
  • 5:42 - 5:44
    Expects a string.
  • 5:44 - 5:45
    Was it modified?
  • 5:45 - 5:46
    Nothing.
  • 5:46 - 5:48
    And what does it return?
  • 5:48 - 5:50
    A string with a greeting.
  • 5:50 - 5:53
    Now this is again,
    pretty bare-bones--
  • 5:53 - 5:54
    but it's a pretty
    bare-bones function.
  • 5:54 - 5:56
    So how much documentation
    do we really need?
  • 5:56 - 5:59
    Notice once again is a triple
    quoted string, even if it's only
  • 5:59 - 6:01
    going to be one line
    long, but you'll typically
  • 6:01 - 6:03
    want to write more than that.
  • 6:03 - 6:06
    After I've written
    this function,
  • 6:06 - 6:09
    now comments were thrown
    out by the byte compiler,
  • 6:09 - 6:11
    but the docstring is
    actually kept around.
  • 6:11 - 6:14
    It's kept around in dunder
    doc on our function object.
  • 6:14 - 6:16
    See, it was just like
    grabbed and put over there.
  • 6:16 - 6:20
    And if I run my function,
    no difference whatsoever.
  • 6:20 - 6:22
    But if I say now
    help on the function,
  • 6:22 - 6:24
    help on hello, we
    will get our help,
  • 6:24 - 6:26
    and we'll find out
    how we should run it
  • 6:26 - 6:29
    and what we should do with it.
  • 6:29 - 6:31
    If you, of course, want, you
    can put in additional stuff
  • 6:31 - 6:32
    in the docstring.
  • 6:32 - 6:34
    And they're docstrings
    for classes and docstrings
  • 6:34 - 6:36
    for modules as well.
  • 6:36 - 6:39
    And most of the code checkers,
    like Pylint and PyFlakes,
  • 6:39 - 6:41
    will actually ding
    you a little bit
  • 6:41 - 6:44
    and scold you if you
    don't include a docstring.
  • 6:44 - 6:45
    So make sure to
    include docstrings
  • 6:45 - 6:47
    on all of your functions,
    modules, and classes.
  • 6:47 - 6:51
    I hope that was a good
    introduction to docstrings
  • 6:51 - 6:51
    versus comments.
  • 6:51 - 6:53
    I hope that you will
    indeed comment your code,
  • 6:53 - 6:56
    and you should also join
    my Better Developers list.
  • 6:56 - 6:58
    It's at
    betterdevelopersweekly.com.
  • 6:58 - 7:00
    Send me questions,
    send me comments.
  • 7:00 - 7:02
    I'd love to hear from
    people around the world,
  • 7:02 - 7:06
    and I'll be back soon with
    more videos and explanations.
Title:
Python comments vs. docstrings: What, how, and why
Description:

more » « less
Video Language:
English
Duration:
07:05

English subtitles

Revisions