Python论坛  - 讨论区

标题:[python-chinese] exec open(filename) in results 这句是什么意思?

2006年01月20日 星期五 14:22

Huang Wei hw1979 at gmail.com
Fri Jan 20 14:22:29 HKT 2006

来自Using Mix-ins with Python的Listing 5:
(http://www.linuxjournal.com/articles/lj/0084/4540/4540l5.html)

results = {}
exec open(filename) in results

执行后results会被填值。有人能解释一下这里的 in 是怎么用的吗

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月20日 星期五 14:29

xxmplus xxmplus at gmail.com
Fri Jan 20 14:29:47 HKT 2006

就是把result里的元素依次填到open函数里去,类似for_each那样

在 06-1-20,Huang Wei<hw1979 at gmail.com> 写道:
> 来自Using Mix-ins with Python的Listing 5:
> (http://www.linuxjournal.com/articles/lj/0084/4540/4540l5.html)
>
> results = {}
> exec open(filename) in results
>
> 执行后results会被填值。有人能解释一下这里的 in 是怎么用的吗
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月20日 星期五 14:31

limodou limodou at gmail.com
Fri Jan 20 14:31:35 HKT 2006

在 06-1-20,Huang Wei<hw1979 at gmail.com> 写道:
> 来自Using Mix-ins with Python的Listing 5:
> (http://www.linuxjournal.com/articles/lj/0084/4540/4540l5.html)
>
> results = {}
> exec open(filename) in results
>
> 执行后results会被填值。有人能解释一下这里的 in 是怎么用的吗
>

6.14 The exec statement

exec_stmt ::= "exec" expression ["in" expression ["," expression]]

Download entire grammar as text.
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, the code that's executed is
expected to be be valid as file input (see section 8.2, ``File input''). Be
aware that the return and yield statements may not be used outside of
function definitions even within the context of code passed to the exec
statement.

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, they are used for the global and
local variables, respectively. If provided, locals can be any mapping
object. Changed in version 2.4: formerly locals was required to be a
dictionary.

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__ (!).

Programmer's hints: dynamic evaluation of expressions is supported by the
built-in function eval(). The built-in functions globals() and locals()
return the current global and local dictionary, respectively, which may be
useful to pass around for use by exec.



--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060120/29066876/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月20日 星期五 14:32

limodou limodou at gmail.com
Fri Jan 20 14:32:18 HKT 2006

在06-1-20,xxmplus <xxmplus at gmail.com> 写道:
> 就是把result里的元素依次填到open函数里去,类似for_each那样
>

不对啊。看我的邮件。


--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月20日 星期五 14:33

Huang Wei hw1979 at gmail.com
Fri Jan 20 14:33:45 HKT 2006

但是执行前results的内容是空的,执行后results被填值了呀。如果是for-each的话result不是应该保持内容不变吗?


在 06-1-20,xxmplus<xxmplus at gmail.com> 写道:
> 就是把result里的元素依次填到open函数里去,类似for_each那样
>
> 在 06-1-20,Huang Wei<hw1979 at gmail.com> 写道:
> > 来自Using Mix-ins with Python的Listing 5:
> > (http://www.linuxjournal.com/articles/lj/0084/4540/4540l5.html)
> >
> > results = {}
> > exec open(filename) in results
> >
> > 执行后results会被填值。有人能解释一下这里的 in 是怎么用的吗
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月20日 星期五 14:36

Huang Wei hw1979 at gmail.com
Fri Jan 20 14:36:25 HKT 2006

明白了,还是看手册不够仔细阿 :)

在 06-1-20,limodou<limodou at gmail.com> 写道:
> 在 06-1-20,Huang Wei<hw1979 at gmail.com> 写道:
> > 来自Using Mix-ins with Python的Listing 5:
> >
> (http://www.linuxjournal.com/articles/lj/0084/4540/4540l5.html
> )
> >
> > results = {}
> > exec open(filename) in results
> >
> > 执行后results会被填值。有人能解释一下这里的 in 是怎么用的吗
> >
>
> 6.14 The exec statement
>
> exec_stmt ::= "exec" expression ["in" expression ["," expression]]
>
> Download entire grammar as text.
> 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, the code that's executed is
> expected to be be valid as file input (see section 8.2, ``File input''). Be
> aware that the return and yield statements may not be used outside of
> function definitions even within the context of code passed to the exec
> statement.
>
> 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, they are used for the global and
> local variables, respectively. If provided, locals can be any mapping
> object. Changed in version 2.4: formerly locals was required to be a
> dictionary.
>
>  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__ (!).
>
> Programmer's hints: dynamic evaluation of expressions is supported by the
> built-in function eval(). The built-in functions globals() and locals()
> return the current global and local dictionary, respectively, which may be
> useful to pass around for use by exec.
>
>
>
> --
> I like python!
> My Blog: http://www.donews.net/limodou
> NewEdit Maillist: http://groups.google.com/group/NewEdit
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月20日 星期五 14:38

xxmplus xxmplus at gmail.com
Fri Jan 20 14:38:25 HKT 2006

i c

在 06-1-20,Huang Wei<hw1979 at gmail.com> 写道:
> 但是执行前results的内容是空的,执行后results被填值了呀。如果是for-each的话result不是应该保持内容不变吗?
>
>
> 在 06-1-20,xxmplus<xxmplus at gmail.com> 写道:
> > 就是把result里的元素依次填到open函数里去,类似for_each那样
> >
> > 在 06-1-20,Huang Wei<hw1979 at gmail.com> 写道:
> > > 来自Using Mix-ins with Python的Listing 5:
> > > (http://www.linuxjournal.com/articles/lj/0084/4540/4540l5.html)
> > >
> > > results = {}
> > > exec open(filename) in results
> > >
> > > 执行后results会被填值。有人能解释一下这里的 in 是怎么用的吗
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号