ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

7/44 注释 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

7/44 注释

Comments | Python for Beginners [7 of 44]

注释
Now, before you get too far into coding with your code,
现在 在你开始写代码之前
it’s useful to get in the habit right away of using comments.
最好先养成使用注释的习惯
Comments are a way of documenting your code.
注释是给代码加文档的一个途径
By adding a hashtag or a pound symbol
在代码的开头
at the beginning of the line of code,
加一个标签也就是井字符
that turns your line into a comment,
就把这行代码变成了注释
which means when the code runs,
也就意味着代码运行的时候
it does absolutely nothing.
什么都不会发生
It’s there for you as a programmer to remember what your code does
作为程序员 它会帮你记住代码功能
or for someone else reading your code to understand what your code does.
或者帮阅读代码的其他人理解代码功能
So when you run the code,
这样一来 当你运行代码时
you aren’t going to see any output in this case.
不会有任何输出
In fact, you can put comments in front of code
事实上 你可以在正在测试的代码前
that you’re experimenting with.
加一些注释
Maybe you try a line of code
当你想修改某处时
it isn’t working or it is working,
可以试着只改一句
when you want to try changing something.
看看是否有效
Rather than deleting the line of code,
有时候我会把代码注释掉
sometimes I’ll comment out that line of code
再替换成不同的代码
and then try it again with the difference.
而不是把代码直接删掉
So that way, I don’t lose the old code,
这样的话 以前的代码不会丢失
I just comment it out in case I need it back.
我只是把它注释掉以备不时之需
So they are useful.
所以这个功能非常有用
So any line of code that has
任何一行
that pound or hashtag symbol in front of it,
带有井字符号的代码
is not going to execute.
都不会被执行
A really good habit to get into is
当你调用一个函数或者一个方法时
if you’re calling a function or a method,
有一个非常好的习惯
and we’re going to cover functions and methods later on in the course.
我们后续课程会讲到函数和方法
But if you’re calling one,
当你调用它们的时候
add a comment before you call it to remind yourself,
调用之前添加一个注释
what does this function do?
这个函数功能是什么?
Why am I calling it here?
为什么要在这里调用?
It might be really obvious to you
在你一开始写代码的时候
when you’re writing the code initially,
可能觉得这么做没有必要
but when you come back to that code in six months,
但当你过半年 一年甚至两年后
or a year, or two years to fix a bug or to make a change,
回过头来调试代码或者修改代码
chances are you’re going to forget a lot of the code you did.
可能你就把这段代码忘记了
So you’ll really appreciate the time you spent
所以你会非常庆幸最初写代码的时候
commenting when you did the initial work on it,
花时间进行了注释
when everything was fresh in your mind.
因为那时你对所有地方都很清楚
Sometimes you’ll have to do some quirky things
有时为了让代码正常运行
to make things work.
你要做一些奇怪的事
Maybe you’re always using single quotes around all your strings,
比如你一直用单引号括住字符串
and then suddenly you have one print statement with double-quotes.
突然有一句用了双引号
Why did you do that?
为什么要这么做呢?
Well, maybe I should add a little note.
也许可以加一点说明
If you’re printing a string like it’s a small world after all,
比如你打算输出字符串 it’s a small world after all
there’s a single quote inside that string.
字符串里已经有一个单引号
So I would have to use double quotes to enclose the string.
因此需要双引号把它括起来
So what I’ve done, is I’ve added a comment here,
所以我在这里加了一个注释
so anybody else who was reading my code
其他人看我的代码时
would understand why is it I use single quotes
也能理解为什么
for all my strings except this one line.
我只在这里用了双引号
So those are the things
所以当你
when you do something out of the ordinary or make a function call,
做的事和平时不太一样或调用函数的时候
add comments so others can understand your code.
加注释可以方便别人理解
It’s also really useful when you are debugging your code as well.
在调试代码的时候注释也很重要
If you’ve got a long program and something is blowing up
假如你的程序很长并且出了问题
but you can’t figure out which line of code isn’t working.
但你不知道是哪行代码出了问题
There have absolutely been times
我确实遇到过这种情况
where what I did was I basically commented out.
我一般会把代码注释掉
So turned a whole bunch of lines of code into comments,
就是把一大堆代码注释掉
and then see if the first five lines of code in my program worked.
再看前五行能不能正常运行
The first five lines worked,
如果前五行没有问题
let’s uncomment the next five lines.
就接着看下面五行
Did that work? Still no error message.
可以运行吗?如果依然不报错
Uncomment the next five lines,
继续看接下来的五行
and so on until I see the error message.
直到看到报错信息为止
That can help me narrow down
这么做可以帮我
where in the code my error is coming from.
缩小报错来源的范围
So get in the habit right away of using comments in your code,
不论是用来记录你的工作还是调试代码
both for documenting what you’ve done and for debugging.
马上养成在代码中加注释的习惯吧

发表评论

译制信息
视频概述

本视频介绍了python中的注释功能,初学者最好养成注释的习惯,既可以记录代码也可以调试代码

听录译者

收集自网络

翻译译者

一颗大橙子

审核员

审核员1024

视频来源

https://www.youtube.com/watch?v=kEuVvUc1Zec

相关推荐