python - 为什么我应该“谨慎使用内联注释”?
问题描述
阅读 PEP8(https://www.python.org/dev/peps/pep-0008/#comments),我看到程序员应该“谨慎使用内联注释”。但是,没有提供任何理由。
我想知道为什么。我真的发现通过阅读带有内联注释的代码更容易理解程序,这样当我真正尝试阅读和理解代码时,我就不会被注释打断。
最好的情况是像我们在 Word 中所做的那样,与代码中的某个点匹配,但仅在侧面板中可见。不知道有没有这样功能的IDE。
话虽这么说,问题:
是否有技术原因(例如性能损失)更喜欢在块中而不是内联中进行注释?
阅读我的代码的人是否会对许多内联注释感到生气,或者这没什么大不了的,前提是这些注释实际上是有用的。
有没有更好的评论环境的IDE?可能是侧面板,也可能是与 .py 文件绑定的 .txt 文件,并通过其 UI 显示这些注释。Idk,任何东西。
阅读 PEP、Python 文档并尝试使用谷歌搜索。没有有用的答案。
(对不起,如果太糟糕了。有点像 Python 3 的初学者。)
guessed_char = len(secret_word)*["_ "] # Defines varLIST of STRs, masked version of
# secret_word, and also the progression
# shown to the player
answer = len(secret_word)*[""] # Defines varLIST of STRs, that will be checked
# against secret_word (varSTR) through
# "".join(answer)
guessed_attempts = [] # Defines varLIST of STRs, that will store
# every letter guessed by the player.
enforcou = False # Defines varBOOL, turns True if attempts
# (varINT) reaches the limit. Ends the game
# with a loss.
acertou = False # Defines varBOOL, turns True if word is
# found by player. Ends the game with a win.
pontos = int(100) # Defines varINT, counts points that will be
# deduced at every failend attempt.Every
# change to this value must be followed by
# changes in other parts of this code
# Asks player for difficulty level
print("Qual nível de dificuldade?\n 1 - 3 tentativas\n 2- 5 tentativas\n 3 - 10 tentativas")
dificuldade = int(input())
if (dificuldade == 1):
max_attempts = 3 # Define varINT, total number of attempts
elif (dificuldade == 2):
max_attempts = 5 # Define varINT, total number of attempts
else:
print("Vamos jogar no fácil...")
max_attempts = 10 # Define varINT, total number of attempts
attempts = 1 # Define varINT, counts the number of attempts
print("".join(guessed_char)) # Prints the masked version of secret_word
# (varSTR) that the player is trying to guess
while (not enforcou and not acertou): # Conditions for ending the game.
print(f"Tentativa {attempts} de {max_attempts}") # Tells player the attempt number
guess = input("Qual letra? ").lower() # Asks for a guess (varSTR)
guessed_attempts.append(guess) # Adds the guess to the guess' storage
if (guess in secret_word): # If the guess is correct
index = 0 # Defines varINT for storing indexes
for char in secret_word.lower(): # iterate through every character in
# secret_word (varSTR)
if (guess == char): # If the guess matches the character
guessed_char[index] = f"{guess} " # Replaces dummy "_ " in
# guesed_char (varLIST)
# revealling guessed characters
# in the masked progresion shown
# to the player
answer[index] = f"{guess}" # Replaces dummy with answer
print(f"Encontrei a letra {char} na posiçao {index}") # Prints that something was correct
print("".join(guessed_char)) # Prints the update masked word
pontos += round(100/max_attempts) # Adds points
if("".join(answer) == secret_word): # Checks if the player won
acertou = True # End while cicle if player won
print(f"Você ganhou! A palavra era '{secret_word}'!") #Prints message telling player he/she won
index += 1 # Adds 1 to varINT index, so everything is on track
else: # If guess is not correct
pontos -= round(100/max_attempts) # Subtracts points
print("Que pena, você errou...") #Tells player he missed
print("".join(guessed_char)) # Print masked word
# Tells player the guesses he already tried, with correct grammar for linstings in portuguese
print("Você já tentou as letras ", ", ".join(str(e) for e in guessed_attempts[:-1]), " e ", guessed_attempts[-1], ".", sep="")
attempts += 1 # Adds 1 to varINT attempts, so the game get closer to end due to loss
enforcou = attempts == max_attempts+1 # Defines coditions for losing the game
if (enforcou): # If losing conditions are met
print(f"Que pena, você perdeu... A palavra era '{secret_word}'...") # Print loss message and the word
print(f"Você fez {pontos} pontos.") # Prints the number of points, regardless of winning or losing.
print ("Fim do jogo") # Tells the player the game ended.
解决方案
我认为反对内联注释的一个简单理由是最大行长度为 79 个字符https://www.python.org/dev/peps/pep-0008/#id17,因此您不想将代码包装太多以适应内联注释。
此外,您还有其他方法可以添加注释,例如 docstrings ( https://www.python.org/dev/peps/pep-0257/ ),这些注释可能涵盖变量的作用。
推荐阅读
- sql - 你能把一个变量传递给 DATEDIFF 的区间吗
- c++ - 如何列出 Cereal 找到的序列化函数
- reactjs - TypeError:无法读取是的未定义的属性“对象”
- javascript - jQuery 使用许多多选字段进行验证(使用 Django)
- python - NaN 在 scikit-learn 的 OneHotEncoder 中给出 ValueError
- c# - 具有不同类型路由值的 Web Api 中的路由
- r - 如何在 R 中制作一组累积递增的整数?
- c - 递归函数内循环的时间复杂度
- c++ - C++ 中的时序逻辑错误(使用 std::chrono)
- r - 计算 r 中簇内的平均值