Python中的代码块 [英] code blocks in Python

查看:90
本文介绍了Python中的代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道人们之前已经谈过这个问题,但是我仍然因为阅读旧帖而感到困惑。


当我谈论代码块时,我不是在谈论Lisp / Ruby / Perl,

所以我没有进行比较。


如果你有一个文件,你可以用Python:


code = compile(code_string,''FileName.py'',''exec'')


并获取一个代码对象,稍后您可以执行该代码对象。那是我想到的那种代码块。

。也就是说,没有参数,没有

范围的规范,没有别的。只是一个简单的代码块

行编译成代码对象。


编写代码字符串并不是太多工作: />

my_codeblock =''''''

x = x + 1

y = y + 2

'$'''


然后使用compile()将其转换为代码对象。但似乎我认为Python可以完全允许这种语法直接进入

语言,例如:


codeblock my_codeblock :

x = x + 1

y = y + 2


这样用户就不需要显式调用编译()函数。

优点是代码块在编译时编译,而不是在运行时编译
,即使代码块是在其他内容中定义的(a br />
类,一个函数等。)

我有什么明显的东西在这里丢失吗?为什么不编码

语言的一部分?它似乎是

元编程的重要构建元素。不仅如此,它似乎比功能更为基础

。那么,为什么它不是Python的一部分呢?任何内在的

困难?


问候,


Hung Jung

解决方案



Hung Jung Lu <胡******** @ yahoo.com>在消息中写道

新闻:8e ************************* @ posting.google.co m ... < blockquote class =post_quotes>

我知道人们之前已经谈过这个问题,但我仍然对阅读旧信息感到困惑。

当我谈论代码块时,我不是在谈论Lisp / Ruby / Perl,
所以我没有进行比较。

如果你有一个文件,用Python可以做到:

code = compile(code_string,''FileName.py'',''exec'')

并获取一个代码对象,稍后您可以执行。这就是我想到的那种代码块。也就是说,没有参数,没有范围的规范,没有别的。只是一个简单的代码块
行编译成代码对象。

编写如下代码字符串并不算太多:

my_codeblock = ''''''
x = x + 1
y = y + 2
'''''

然后使用compile()转它成为一个代码对象。但似乎我认为Python可以完全允许这种语法直接进入
语言,例如:

codeblock my_codeblock:
x = x + 1
y = y + 2

这样用户就不需要显式调用compile()函数。
优点是代码块是在编译时编译的,而不是
在运行时,即使代码块是在其他内容中定义的(
类,函数等)

有什么明显的东西我在这里丢失吗?为什么不编码语言的一部分?它似乎是元编程的重要构建元素。不仅如此,它似乎比功能更基础。那么,为什么它不是Python的一部分呢?任何内在的困难?


只是一些随机观察。首先,我不清楚

你想要它做什么。一些例子会有所帮助。

完全是太多不同的方式我可以解释你的参考

元编程让我理解你在

心中的用法。 br />

如果你想把它作为严重瘫痪的lambda声明的替代品,那么我认为这是非常有趣的。什么是持有这个是

语法,并且一个合适的语法并不是完全明显的。


如果你想要它做其他事情,喜欢这样的行为实际上是编译它将被调用的地方,标识符分辨率

和所有,然后这是一个完全不同的主题。


John Roth


问候,

Hung Jung



John Roth <是ne ******** @ jhrothjr.com>在消息新闻中写道:< vr ************ @ news.supernews.com> ...

只是一些随机观察。首先,我不清楚你想要它做什么。一些例子会有所帮助。有很多不同的方式我可以将你的参考解释为元编程,让我理解你在脑海中的用法。


codeblock A:

x = 1

y = 2


codeblock B:

z = x + y

打印z

exec A

exec B


这相当于编写程序:


x = 1

y = 2

z = x + y

打印z

如果你想要它作为严重残缺的lambda声明的替代品,那么我认为有相当多的利益。什么是这样的语法,并且适当的语法并不是完全明显的。




Lambda是一个完全不同的东西。 Lambda是一个匿名的

*函数*。我强调* function *因为函​​数接受参数

并返回值。代码块就是代码块。没有

参数,没有别的。简单明了的代码块。一旦你有
有代码块,你就可以随意连接它们。把它们想象成C ++中的
宏,只有在Python中你可以在运行时动态地连接它们b / b
。 Python已经做到了。这只是你需要

compile()函数,你的代码字符串不会被编译,直到执行

compile()函数,以及生成的对象类型是

"代码" (这是一个好名字,即使新关键字是

" codeblock" ;.)你有没有尝试过compile()函数?


问候,


Hung Jung


2003年11月22日19:13:19 -0800, hu ******** @ yahoo.com (Hung Jung Lu)写道:

" ;约翰罗斯 <是ne ******** @ jhrothjr.com>在消息新闻中写道:< vr ************ @ news.supernews.com> ...

只是一些随机观察。首先,我不清楚你想要它做什么。一些例子会有所帮助。有很多不同的方式我可以将你的参考解释为元编程,让我理解你在脑海中的用法。



codeblock答:
x = 1
y = 2

代码块B:
z = x + y
打印z

exec A
exec B

这相当于编写程序:

x = 1
y = 2
z = x + y
打印z

如果你想把它作为严重瘫痪的lambda声明的替代品,那么我认为有相当多的兴趣。什么是持有它的语法,并且适当的语法并不是完全明显的。



Lambda是完全不同的东西。 Lambda是一个匿名的
*功能*。我强调* function *因为函​​数接受参数
并返回值。代码块就是代码块。没有
参数,没有别的。简单明了的代码块。一旦你有了代码块,你就可以随意连接它们。把它们想象成C ++中的宏,只有在Python中你可以在运行时动态地连接它们。 Python已经做到了。这只是你需要
compile()函数,你的代码字符串不会被编译,直到执行
compile()函数,并且生成的对象类型是
Code ; (这是一个很好的名字,即使新关键字是
codeblock。)您是否尝试过compile()函数?



我注意到:

compile(''a = 123'','''',''single'')
<代码对象?在0090D5A0,文件",第1行> import dis
dis.dis(compile(''= 123'','''',''single''))
1 0 LOAD_CONST 0(123)

3 STORE_NAME 0(a)

6 LOAD_CONST 1(无)

9 RETURN_VALUE def foo():a = 123
... dis.dis( foo)



1 0 LOAD_CONST 1(123)

3 STORE_FAST 0(a)

6 LOAD_CONST 0(无)

9 RETURN_VALUE


当编译器编译上下文块时,您需要哪些代码?

您希望它成为一流的物体吗?你是否应该能够将代码块名称传递给任意函数或将其全局绑定,就像你可以使用编译输出一样使用
?您是否愿意放弃优化的本地访问?或者想要以后从任何地方执行
执行吗?应该怎么办?您可以调用魔术创建特殊的

闭包,这样导出的代码块仍然可以返回到本地名称空间

(对于生成器来说BTW可能很有用,允许外部访问他们的

内部状态)。什么应该将函数本地代码块分配给全局均值?

也许创建一次性生成器,其中代码块可用于预设状态为
a单个普通函数调用重新使用以前的状态。或者它应该像

弱参考那样死?


IOW,有一些事情要考虑,因为语义可能会变得有趣; - )

拥有高效的本地块会很好,所以你可以编写exec(locals()[switcharg])

或者使用它们可能是一些更有效的内置开关/盒子的东西。

a大跳转表(基于dict或元组/列表)与一系列精灵的效率对于某些用途是一个很好的动机,

但是我想知道多么常见/重要他们是。


问候,

Bengt Richter


Hi,

I know people have talked about it before, but I am still really
confused from reading the old messages.

When I talk about code blocks, I am not talking about Lisp/Ruby/Perl,
so I am not making comparisons.

If you have a file, in Python you could do:

code = compile(code_string, ''FileName.py'', ''exec'')

and obtain a code object, which later on you can execute. That''s the
kind of code block I have in mind. That is, no parameters, no
specification of scopes, no nothing else. Just a plain block of code
lines compiled into a code object.

It''s not too much work to write a code string like:

my_codeblock = ''''''
x = x + 1
y = y + 2
''''''

and then use compile() to turn it into a code object. But it seems to
me that Python could perfectly allow this syntax directly into the
language, something like:

codeblock my_codeblock:
x = x + 1
y = y + 2

so that users won''t need to explicitly call the compile() function.
The advantage is that the codeblock is compiled at compile time, not
at run time, even if the codeblock is defined inside something else (a
class, a function, etc.)

Is there something obvious that I am missing here? Why isn''t codeblock
part of the language? It seems to be an important building element for
meta-programming. Not only that, it seems to be much more fundamental
than functions. So, why isn''t it part of Python? Any intrinsic
difficulties?

regards,

Hung Jung

解决方案


"Hung Jung Lu" <hu********@yahoo.com> wrote in message
news:8e*************************@posting.google.co m...

Hi,

I know people have talked about it before, but I am still really
confused from reading the old messages.

When I talk about code blocks, I am not talking about Lisp/Ruby/Perl,
so I am not making comparisons.

If you have a file, in Python you could do:

code = compile(code_string, ''FileName.py'', ''exec'')

and obtain a code object, which later on you can execute. That''s the
kind of code block I have in mind. That is, no parameters, no
specification of scopes, no nothing else. Just a plain block of code
lines compiled into a code object.

It''s not too much work to write a code string like:

my_codeblock = ''''''
x = x + 1
y = y + 2
''''''

and then use compile() to turn it into a code object. But it seems to
me that Python could perfectly allow this syntax directly into the
language, something like:

codeblock my_codeblock:
x = x + 1
y = y + 2

so that users won''t need to explicitly call the compile() function.
The advantage is that the codeblock is compiled at compile time, not
at run time, even if the codeblock is defined inside something else (a
class, a function, etc.)

Is there something obvious that I am missing here? Why isn''t codeblock
part of the language? It seems to be an important building element for
meta-programming. Not only that, it seems to be much more fundamental
than functions. So, why isn''t it part of Python? Any intrinsic
difficulties?
Just some random observations. First, I''m not at all clear on
what you want it to do. Some examples would be helpful. There
are entirely too many different ways I could interpret your reference
to metaprogramming for me to understand the usage you have in
mind.

If you want it as a replacement for the badly crippled lambda statement,
then I think there''s quite a bit of interest. What''s holding that up is
syntax, and an appropriate syntax isn''t entirely obvious.

If you want it to do something else, like act like it was actually
compiled where it will be invoked, with identifier resolution
and all, then that''s a completely different subject.

John Roth


regards,

Hung Jung



"John Roth" <ne********@jhrothjr.com> wrote in message news:<vr************@news.supernews.com>...

Just some random observations. First, I''m not at all clear on
what you want it to do. Some examples would be helpful. There
are entirely too many different ways I could interpret your reference
to metaprogramming for me to understand the usage you have in
mind.
codeblock A:
x = 1
y = 2

codeblock B:
z = x + y
print z

exec A
exec B

This will be equivalent to writing the program:

x = 1
y = 2
z = x + y
print z
If you want it as a replacement for the badly crippled lambda statement,
then I think there''s quite a bit of interest. What''s holding that up is
syntax, and an appropriate syntax isn''t entirely obvious.



Lambda is a totally different thing. Lambda is an anonymous
*function*. I emphasize *function* because functions take parameters
and return values. Code blocks are, well, blocks of code. No
parameters, no nothing else. Simple and plain blocks of code. Once you
have codeblocks, you wire them anyway you want. Think of them as
macros in C++, only that in Python you can wire them dynamically
during runtime. Python already does that. It''s just that you need the
compile() function, your code string is not compiled until the
compile() function is executed, and the resulting object type is
"Code" (which is a fine name, even if the new keyword were
"codeblock".) Have you tried the compile() function?

regards,

Hung Jung


On 22 Nov 2003 19:13:19 -0800, hu********@yahoo.com (Hung Jung Lu) wrote:

"John Roth" <ne********@jhrothjr.com> wrote in message news:<vr************@news.supernews.com>...

Just some random observations. First, I''m not at all clear on
what you want it to do. Some examples would be helpful. There
are entirely too many different ways I could interpret your reference
to metaprogramming for me to understand the usage you have in
mind.



codeblock A:
x = 1
y = 2

codeblock B:
z = x + y
print z

exec A
exec B

This will be equivalent to writing the program:

x = 1
y = 2
z = x + y
print z

If you want it as a replacement for the badly crippled lambda statement,
then I think there''s quite a bit of interest. What''s holding that up is
syntax, and an appropriate syntax isn''t entirely obvious.



Lambda is a totally different thing. Lambda is an anonymous
*function*. I emphasize *function* because functions take parameters
and return values. Code blocks are, well, blocks of code. No
parameters, no nothing else. Simple and plain blocks of code. Once you
have codeblocks, you wire them anyway you want. Think of them as
macros in C++, only that in Python you can wire them dynamically
during runtime. Python already does that. It''s just that you need the
compile() function, your code string is not compiled until the
compile() function is executed, and the resulting object type is
"Code" (which is a fine name, even if the new keyword were
"codeblock".) Have you tried the compile() function?


I note that:

compile(''a=123'','''',''single'') <code object ? at 0090D5A0, file "", line 1> import dis
dis.dis(compile(''a=123'','''',''single'')) 1 0 LOAD_CONST 0 (123)
3 STORE_NAME 0 (a)
6 LOAD_CONST 1 (None)
9 RETURN_VALUE def foo(): a=123 ... dis.dis(foo)


1 0 LOAD_CONST 1 (123)
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE

So which code will you want when the compiler compiles an in-context block?
Will you want it to be a first-class object? Should you be able to
pass the code-block name to an arbitrary function or bind it globally, like you can
with compile''s output? Are you willing to forego the optimized local access? Or want to
exec it later from anywhere? What should happen? You could call for magic creation of special
closures so that an exported code block would still refer back to the local name space
(which BTW could be interesting for generators to yield, to give external access to their
internal state). And what should assigning a function local codeblock to a global mean?
Maybe creating a one-shot generator where the codeblock could be used to preset state for
a single ordinary function call that re-uses previous state. Or should it just die like a
weak reference?

IOW, there are some things to think about, because the semantics could get interesting ;-)
It would be nice to have efficient local blocks, so you could write exec(locals()[switcharg])
or perhaps some more efficient builtin switch/case thing using them. The efficiency of
a large jump table (dict or tuple/list based) vs a chain of elifs is a good motivation for some uses,
but I wonder how common/important they are.

Regards,
Bengt Richter


这篇关于Python中的代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆