任何允许在python中使用匿名代码块的技巧? [英] any trick to allow anonymous code blocks in python?

查看:104
本文介绍了任何允许在python中使用匿名代码块的技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何元类技巧或类似的东西允许匿名

代码块?


我希望能够让用户做某事像这个虚构的例子:

b = Button()

b.OnClick =:

print" you clicked me


但是这需要添加一个特殊的=:运算符绑定一个代码

块到一个函数。

这样的东西有没有PEP?我看到310可能允许:

b =按钮()

与b.OnClick:

打印你点击了我


我知道我已经可以像这些例子那样做了:

def OnClick(自我,事件):

print" you clicked me

b.OnClick = OnClick



b =按钮(OnClick = OnClick)

或子类化按钮。

Is there any metaclass trick or something similar to allow anonymous
code blocks?

I''d like to be able to let users do something like this fictitious example:
b = Button()
b.OnClick =:
print "you clicked me"

But that would require adding a special "=:" operator to bind a code
block to a function.
Is there any PEP for something like that? I see 310 might allow:
b=Button()
with b.OnClick:
print "you clicked me"

I know I can already do it like these examples:
def OnClick(self,event):
print "you clicked me"
b.OnClick = OnClick
or
b = Button(OnClick=OnClick)
or subclassing Button.

推荐答案

" Doug Holton" <在**** @ spam.here>在消息中写道

news:v6 ******************** @ comcast.com ...
"Doug Holton" <in****@spam.here> wrote in message
news:v6********************@comcast.com...
是有没有任何元类技巧或类似的东西允许匿名
代码块?

我希望能够让用户做这样的虚构
示例:b = Button ()
b.OnClick =:
打印你点击了我
Is there any metaclass trick or something similar to allow anonymous
code blocks?

I''d like to be able to let users do something like this fictitious example: b = Button()
b.OnClick =:
print "you clicked me"




编号如果您正在寻找GUI回调, lambdas和绑定方法之间存在显着的差距。你可以

使用像(未经测试)的东西:


b.OnClick =(lambda:sys.stdout(你点击了我))


哪个会起作用(假设我的语法正确)。


如果你做的不是玩具程序但是,最好的方法是将GUI小部件包装在一个类中,然后使用该类的绑定方法进行回调。


Python库的Tkinter章节中的示例

参考(Python 2.3.3文档中的16.1.2.2)显示了

如何做到这一点。它非常简单,它完全以对象的形式定向,并且它让您可以完全访问回调中的实例。


John Roth



No. If you''re looking for GUI callbacks, there''s a significant
gap between lambdas and bound methods. You could
use something like (not tested):

b.OnClick = (lambda : sys.stdout("You clicked me"))

which will work (assuming I''ve got the syntax right).

If you''re doing anything other than a toy program, though,
the best approach is to wrap the GUI widget in a class,
and use a bound method of that class for the callbacks.

The example in the Tkinter chapter of the Python Library
Reference (16.1.2.2 in the Python 2.3.3 docs) shows
how to do it. It''s amazingly simple, it''s completely object
oriented, and it gives you full access to the instance within
the callback.

John Roth


lambda?
lambda?


Tyler Eaves写道:
Tyler Eaves wrote:
lambda?




看起来,因为它没有在

函数上添加名称。但是无论如何都会创建一个函数对象

,而不仅仅是一个代码对象。

类似于使用未命名的lambda是

定义一个函数,将它绑定到某些上下文中,

并立即删除它。

原则上没有区别。


-

Christian Tismer:^)< mailto:ti **** @ stackless.com>

Mission Impossible 5oftware:休息一下!搭乘Python'

Johannes-Niemeyer-Weg 9a:* Starship * http://starship.python.net/

14109柏林:PGP密钥 - > http://wwwkeys.pgp.net/

work +49 30 89 09 53 34主页+49 30 802 86 56手机+49 173 24 18 776

PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04

谁你想今天赞助吗? http://www.stackless.com/



Looks like, since it does not put a name on the
function. But there is anyway a function object
created, not just a code object.
The similar to using an unnamed lambda is
defining a function, bindin it into some context,
and deleting it immediately.
There is no difference, in principle.

--
Christian Tismer :^) <mailto:ti****@stackless.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python''s
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/


这篇关于任何允许在python中使用匿名代码块的技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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