• 手机站
  • 收藏
聚培教育网合作机构 > 深圳博为峰教育
深圳博为峰教育
400-998-6158
深圳博为峰教育

python中怎么写注释

python学习网

更新时间:2023-01-10 浏览:159
核心提示:python中怎么写注释注释语法Python中的注释以哈希标记( # )和空格字符开头,并继续到行的结尾。 一般来说,注释看起来像这样:

python中怎么写注释

注释语法
Python中的注释以哈希标记( # )和空格字符开头,并继续到行的结尾。 一般来说,注释看起来像这样:
# This is a comment
因为注释不执行,当你运行一个程序,你不会看到任何指示的评论。注释在源代码中供人阅读,而不是由计算机执行。 在“Hello,World!”程序中,注释可能如下所示:
hello.py
# Print “Hello, World!” to console
print("Hello, World!")
在迭代列表的for循环中,注释可能如下所示:
sharks.py
# Define sharks variable as a list of strings
sharks = ['hammerhead', 'great white', 'dogfish', 'frilled', 'bullhead', 'requiem']
# For loop that iterates over sharks list and prints each string item
for shark in sharks:
print(shark)
注释应该与注释的代码相同。也就是说,没有缩进的函数定义将具有没有缩进的注释,并且每个缩进级别将具有与其注释的代码对齐的注释。 例如,下面是如何在Python 3教程中如何使一个简单的计算器程序的again()函数被注释,在代码的每个缩进级别后面的注释:
calculator.py
...
# Define again() function to ask user if they want to use the calculator again
def again():
# Take input from user
calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for
''')
# If user types Y, run the calculate() function
if calc_again == 'Y':
calculate()
# If user types N, say good-bye to the user and end the program
elif calc_again == 'N':
print('See you later.')
# If user types another key, run the function again
else:
again()
注释用于帮助程序员,无论是原始程序员还是其他人在项目中使用或协作。如果注释不能与代码库一起正确维护和更新,不要包含注释,而不要编写与代码相矛盾或将与代码冲突的注释。 当评论代码时,你应该寻找回答代码背后的原因 ,而不是什么或如何 。除非代码特别棘手,看代码通常可以告诉代码正在做什么或如何做。
块注释
块注释可以用来解释更复杂的代码或代码,你不希望读者熟悉。这些较长形式的注释适用于随后的部分或全部代码,并且也缩进为与代码相同的级别。 在块注释中,每行以哈希标记和单个空格开头。如果您需要使用多个段落,它们应该由包含单个散列标记的线分隔。 下面是一个块注释的例子,它定义了下面定义的main()函数中发生的事情:
# The main function will parse arguments via the parser variable. These
# arguments will be defined by the user on the console. This will pass
# the word argument the user wants to parse along with the filename the
# user wants to use, and also provide help text if the user does not
# correctly pass the arguments.
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"word",
help="the word to be searched for in the text file."
)
parser.add_argument(
"filename",
help="the path to the text file to be searched through"
)
...
块注释通常在操作不那么直接并因此需要彻底解释时使用。您应该尽量避免对代码进行过度注释,并且应该倾向于信任其他程序员来理解Python,除非您是为特定受众编写的。
内联注释
内联注释发生在语句的同一行,跟在代码本身后面。像其他注释一样,它们以哈希标记和单个空格字符开头。 一般来说,内联注释看起来像这样:
[code] # Inline comment about the code
应该谨慎使用内联注释,但可以有效地解释代码的棘手或非显而易见的部分。如果您认为您可能不记得以后编写的代码行,或者您与知道的人员可能不熟悉代码的所有方面进行协作,它们也可能很有用。 例如,如果你不在你的Python程序中使用大量的数学,你或你的协作者可能不知道以下内容创建了一个复数,所以你可能想包括一个在线注释:
z = 2.5 + 3j # Create a complex number
内联注释也可以用来解释背后做什么的原因,或者一些额外的信息,如:
x = 8 # Initialize x with an arbitrary number
只有在必要时,并且当他们可以为阅读程序的人提供有用的指导时,才应使用一致的评论。
注释测试代码
除了使用注释作为文档代码的方式之外,哈希标记还可以用于注释掉在您正在创建的程序测试或调试期间不想执行的代码。也就是说,当您在实施新代码之后遇到错误时,您可能需要评论其中的一些,以查看您是否可以排查疑难问题。 使用哈希标记还可以让您在确定如何设置代码时尝试替代方法。例如,你可能决定在Python游戏中使用while循环或for循环,并且可以在测试和确定哪个可能是的时候注释掉一个或另一个:
guess.py
import random
number = random.randint(1, 25)
# number_of_guesses = 0
for i in range(5):
# while number_of_guesses < 5:
print('Guess a number between 1 and 25:')
guess = input()
guess = int(guess)
# number_of_guesses = number_of_guesses + 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
if guess == number:
print('You guessed the number!')
else:
print('You did not guess the number. The number was ' + str(number))
使用哈希标记注释代码可以允许您尝试不同的编程方法,以及通过系统地注释掉和运行程序的一部分来帮助您找到错误的来源。
在Python程序中使用注释有助于使程序对于人类更可读,包括未来的自我。包括相关和有用的适当的注释可以使其他人更容易与您在编程项目上合作,并使代码的价值更明显。 从这里,你可能想阅读关于Python的Docstrings在PEP 257 ,为您提供更多的资源,以正确地记录您的Python项目。

更多>同类资讯
更多>相关课程
顶部