如何在Python代码中添加带有换行符的注释



如何在Python代码中添加带换行符的注释?我想以下不应该起作用。Python的包装器支持换行符吗?

# Program make a simple calculator
# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
print(subtract(6,   # = 6 - (3 * (1 + 2))
multiply(3,   # = 3 * (1 + 2)
add(1, 2))))  # = 1 + 2

后面不能有任何字符,甚至不能有空格。Python有隐含的行延续,因此可以将表达式扩展到多行并添加这样的注释(尽管我不确定它是否会使代码更可读(:

print(subtract(6,  # = 6 - (3 * (1 + 2))
multiply(3,  # = 3 * (1 + 2)
add(1, 2)  # = 1 + 2
)
)
)

最新更新