>>> openMe = type('X',(object,),{})() #since you can't attach extra attributes to object instances.
>>> openMe.py = 42
>>> filename = input("Enter the name of the file of grades: ")
Enter the name of the file of grades: openMe.py
>>> filename
42
>>>
filename = raw_input("Enter the name of the file of grades: ")
raw_input返回一个字符串,而input相当于eval(raw_input())。在
eval("openMe.py")是如何工作的:
Because python thinks that in openMe.py, openMe is an object while
py is its attribute, so it searches for openMe first and if it is
not found then error is raised. If openMe was found then it searches
this object for the attribute py.
正如Ashwini所说,在python2.x中必须使用
raw_input
,因为input
本质上是eval(raw_input())
。在input("openMe.py")
最后似乎去掉了.py
的原因是python试图找到一个名为openMe
的对象并访问它的.py
属性。在在Python 2中使用
raw_input
:raw_input
返回一个字符串,而input
相当于eval(raw_input())
。在eval("openMe.py")
是如何工作的:示例:
^{pr2}$相关问题 更多 >
编程相关推荐