看不懂的py代码

richyhuang 2014-10-18 10:51:00
s1={'hello':1,'u':2}

exec open('test.txt') in s1


在某大神的网站出现过,原始代码是

result={‘hello’,’u’}
exec open(‘test.txt’) in result

selected = [] # temp list to hold matches
fp = open(‘test.txt’)
for line in fp.readlines(): # Py2.2 -> “for line in fp:”
selected.append(line)
del line # Cleanup transient variable

open(‘test.txt’).readlines()

我用2.6.6执行不通过

http://www.lvluo.net/blog/tag/file
...全文
264 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
panghuhu250 2014-10-19
  • 打赏
  • 举报
回复
引用 2 楼 richyhuang 的回复:
可以执行,但是执行结果是s1,变成了一个很大的字典,不知道如何解释
6.13 The exec statement exec_stmt: "exec" expression ["in" expression ["," expression]] This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed. In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively. As a side effect, an implementation may insert additional keys into the dictionaries given besides those corresponding to variable names set by the executed code. For example, the current implementation may add a reference to the dictionary of the built-in module __builtin__ under the key __builtins__ (!).
richyhuang 2014-10-19
  • 打赏
  • 举报
回复
引用 1 楼 notax 的回复:
>>>
>>> open("test.txt")
<_io.TextIOWrapper name='test.txt' mode='r'
>>> open("test.txt").read()
'"1"\n'
>>> s1={'hello':1,'u':2}
>>> exec(open("test.txt").read()) in s1
False
>>>

谢谢回复

result={‘hello’,’u’}
exec open(‘test.txt’) in result

在哪个版本上面可以执行,执行结果是什么

s1={'hello':1,'u':2}

exec open('test.txt') in s1

可以执行,但是执行结果是s1,变成了一个很大的字典,不知道如何解释
richyhuang 2014-10-19
  • 打赏
  • 举报
回复
引用 3 楼 panghuhu250 的回复:
[quote=引用 2 楼 richyhuang 的回复:] 可以执行,但是执行结果是s1,变成了一个很大的字典,不知道如何解释
6.13 The exec statement exec_stmt: "exec" expression ["in" expression ["," expression]] This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed. In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively. As a side effect, an implementation may insert additional keys into the dictionaries given besides those corresponding to variable names set by the executed code. For example, the current implementation may add a reference to the dictionary of the built-in module __builtin__ under the key __builtins__ (!). [/quote]有中文解释吗?
notax 2014-10-19
  • 打赏
  • 举报
回复
>>> >>> open("test.txt") <_io.TextIOWrapper name='test.txt' mode='r' >>> open("test.txt").read() '"1"\n' >>> s1={'hello':1,'u':2} >>> exec(open("test.txt").read()) in s1 False >>>

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧