DocStrings 的请教 python

来源:百度知道 编辑:UC知道 时间:2024/06/25 20:37:32
E:\Python31\Python_workplace>python hello.py

运行示例程序,提示语法错误,何解?

示例程序:
def printMax(x, y):
'''Prints the maximum of two numbers.

The two values must be integers.'''
x = int(x) # convert to integers, if possible
y = int(y)

if x > y:
print (x, 'is maximum')
else:
print (y, 'is maximum')

printMax(3, 5)
print printMax.__doc__

出错信息:
File "hello.py", line 24
print printMax.__doc__
^
SyntaxError: invalid syntax

print(printMax.__doc__)

python3.x中print做了改变是一个函数,所以要用函数的语法。

E:\Python31\.. (又见3.1print错误)

python3.x把print语句改成了print函数, 所以应该:
print(printMax.__doc__)

另外你前面写对了, 为何后面会写错呢?

在fedora 11, 默认安装的python2.6上,正常运行。

直接复制粘贴到vim里面,保存并运行,结果:

(5, 'is maximum')
Prints the maximum of two numbers.

很可能是3.1版做了什么改变吧。