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

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

8/44 注释的示例

Demo: Comments | Python for Beginners [8 of 44]

>> Okay. Now let’s take a look at
好 现在我们来看一看
how to use comments inside our code.
如何在代码中加注释
So a comment is any line of
那么 注释是一行代码
code which starts with that hashtag or pound symbol.
它前面会有井字号 或者磅的符号
So if I save this program here comments.py,
我把这个程序存一下 comments.py
and then I run it Python comments.py
然后我输入 python comments.py
to execute that line of code,
来执行这一段代码
there’s no output.
没有任何输出
Not because it’s not valid program,
不是因为这个程序无效
I can add a print statement here.
我可以在这里加一个print语句
Say that, run the program,
输入这个 然后运行程序
and you will see the print statement does appear.
可以看见print语句的结果显示出来了
So it is recognizing this as a Python file,
所以 它确实是识别到这是python文件
this running my code
并且运行了我的代码
is just comments don’t do anything.
只是注释什么作用都没有
That’s the point of them.
这就是它们存在的意义
We use them to document our code,
我们用它们来记录代码
and sometimes when we have something like an error,
有时候当我们的程序有错误的时候
so in this case “hello it’s a small world”,
比如这里的 ‘hello it’s a small world’
if I save and run this code, it’s going to blow up.
如果我保存并运行它 它会报错
It’s blowing up because it’s contained inside single quotes,
报错的原因是代码里有单引号
and the string itself has a single quote in it.
而字符串里面也有单引号
So if this was inside a larger program,
如果这段代码在大型程序中
I might find myself trying to figure
我可能会一直在找
out which line of code is blowing up.
到底哪条代码导致报错
So I might use that technique of commenting outlines of code
所以 我可能会用注释代码大纲的手法
to help me find the bug.
来帮我找bug
So by simply putting a pound symbol here,
那么 只要在这里加上井号符
or maybe I take a different version of this line of code,
或者这行代码我再换一种写法
and I say well maybe if I did it with double quotes, it would work.
也许我用双引号它就能运行
So I have a version with double quotes
那么 我就有双引号的版本
and a version with single quotes.
和单引号的版本
When I use the version with the double quotes and I run my code,
当我用双引号的版本的时候 运行一下
hey it works
嘿 可以了
and I go “Ha”.
然后 我就想 哈
So is it really just for double quotes and single quotes the problem.
真的只是单引号和双引号的问题?
So I can go here and I can play around,
那么 我就可以点这里 试一下
maybe I’ll comment out that line and
也许我可以把这一行注释起来
uncomment this line and test that again,
取消另一行的注释 再测试一下
and see if indeed, yes,
看一下是不是真是 是的
look at that, it’s the print statement with the single quotes causing error.
看 是用单引号的print语句制造的错误
So this is one of the ways I use
这是我在调试bug时用的方法
commenting and uncommenting inside my debugging itself.
加注释 取消注释
By the way, handy keyboard shortcut because I do this a lot.
顺带提一下 快捷键 我经常用
If you had the cursor on the line of code and you go “Control KC”,
如果你的光标在一行代码上 你可以按Ctrl+K+C
Control K comment, that will take
也就是Ctrl K 注释
a line of code the cursor is on and turn it into a comment.
这样它会把光标所在的那一行变成一个注释
“Control KU, U for uncomment,
Ctrl+K+U U是取消注释
will uncomment a line of code that’s currently set as a comment.
会让注释的代码取消注释
So, get in the habit of using comments in
为了更好的调试bug和写文档
your code both for debugging and for documenting.
赶紧养成写代码用注释的习惯吧

发表评论

译制信息
视频概述

注释的使用示例

听录译者

收集自网络

翻译译者

小达不溜

审核员

审核员1024

视频来源

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

相关推荐