** kwargs自动绑定到变量 [英] Automatic binding of **kwargs to variables

查看:89
本文介绍了** kwargs自动绑定到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个很长的参数列表,从web表单到我的

方法foo(self,** kwargs)


我想避免手动将变量绑定到** kwargs字典中的值

,只是为了保持代码清洁,

我想自动绑定它们


我被建议不要通过向当地人添加东西来做到这一点,例如:


locals()[" x"] = 5

自Python文档说:


警告:此词典的内容不应修改;

更改可能不会影响

解释器使用的局部变量的值。


是否有这样做的方法?也许对象.__ setattr __()或

涉及元类的东西?


因为我可能会触摸一些我不应该的东西(如本地人())在决定走哪条路之前我会给你一些建议。

谢谢,

Lorenzo

推荐答案

" lb ******** @ gmail.com" <磅******** @ gmail.com>写道:
"lb********@gmail.com" <lb********@gmail.com> writes:
我有一个很长的参数列表,从web表格到我的
方法foo(自我,** kwargs)

我想避免手动将变量绑定到** kwargs字典中的值,只是为了保持代码清洁,
我想自动绑定它们

我是建议不要通过向当地人添加内容来实现这一点,例如:

locals()[" x"] = 5

因为Python文档说:
<警告:不应修改此词典的内容;
更改可能不会影响
解释器使用的局部变量值。

有没有办法做这个?也许对象.__ setattr __()或某些涉及元类的东西?


..__ setattr __()将修改实例变量,而不是本地

变量。

因为我可能会去触摸一些东西我不应该(就像当地人())在决定走哪条路之前我会给你一些建议。
I have a very long list of parameters coming from a web form to my
method foo(self, **kwargs)

I would like to avoid manually binding the variables to the values
coming through the **kwargs dictionary, just to keep the code cleaner,
I''d like to bind them automatically

I was adviced against doing it by adding stuff to locals such as:

locals()["x"] = 5

since the Python Docs say:

Warning: The contents of this dictionary should not be modified;
changes may not affect the values of local variables used by the
interpreter.

Is there a way to do this? Maybe object.__setattr__() or something that
involves metaclasses?
..__setattr__() will modify the instance variables, not the local
variables.
As I might go touching something that I shouldn''t (like locals()) I''d
like some advice from you before deciding which way to go.




参考通过字典的关键字参数。按名称查找

值是字典的用途。试图将字典中的名称从字典移动到本地(或全局)命名空间通常是一个坏主意。如果您事先并不知道所有名字,那么您可能会面临词典中的项目与命名空间中合法的

居民之间的冲突。如果你事先知道所有的名字,

你需要确保字典中的键在你期望的

值列表中,所以你需要一个明确的清单。


那就是说,我能想到的最简单的方法就是执行此操作

与exec:


表示名称,值为kwargs.items():

如果名称在(''a'',''list'',''''',''valid'', ''keywords''):

exec"%s =%s" %(名称,值)

else:

引发ValueError,无法识别的关键字 +名字


其他人可能会告诉你,你真的不应该使用exec。


< mike

-

Mike Meyer< mw*@mired.org> http://www.mired.org/home/mwm/

独立的WWW / Perforce / FreeBSD / Unix顾问,电子邮件以获取更多信息。



Reference the keyword arguments through the dictionary. Looking up
values by name is what dictionaries are for. Trying to move the names
from a dictionary into the local (or global) namespace is usually a
bad idea. If you don''t know all the names in advance, you risk
collisions between the items in the dictionary and the legitimate
inhabitants of the namespace. If you do know all the names in advance,
you need to make sure the keys in the dictionary are in the list of
values you expect, so you need an explicit list.

That said, the most straightforward way to do this that I can think of
is with exec:

for name, value in kwargs.items():
if name in (''a'', ''list'', ''of'', ''valid'', ''keywords''):
exec "%s = %s" % (name, value)
else:
raise ValueError, "Unrecognized keyword " + name

Others will probably tell you that you really shouldn''t be using exec.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.


lb ******** @ gmail.com 写道:
lb********@gmail.com wrote:
我有很长的参数列表来自一个web表单给我的方法foo(self,** kwargs)

我想避免手动将变量绑定到** kwargs字典中的值
只是为了让代码更干净,
我想自动绑定它们
I have a very long list of parameters coming from a web form to my
method foo(self, **kwargs)

I would like to avoid manually binding the variables to the values
coming through the **kwargs dictionary, just to keep the code cleaner,
I''d like to bind them automatically




为什么不把方法签名改为foo(自我,x,y,z,

无论如何,** kwargs)?


彼得



Why don''t you just change the method signature to foo(self, x, y, z,
whatever, **kwargs)?

Peter


Peter Otten写道:
Peter Otten wrote:
lb ******** @ gmail .com 写道:

lb********@gmail.com wrote:

我有一个很长的参数列表,从web表单到我的
方法foo(self,** kwargs)

我想避免手动将变量绑定到价值
来自** kwargs字典,只是为了保持代码清洁,
我想自动绑定它们
I have a very long list of parameters coming from a web form to my
method foo(self, **kwargs)

I would like to avoid manually binding the variables to the values
coming through the **kwargs dictionary, just to keep the code cleaner,
I''d like to bind them automatically



为什么不你只需将方法签名更改为foo(self,x,y,z,
等等,** kwargs)?

Peter


Why don''t you just change the method signature to foo(self, x, y, z,
whatever, **kwargs)?

Peter



可能因为那些参数需要值。另外,要指定非常长的清单,需要

来维护。

但是,我必须同意Mike的建议:尝试使用任意变量来调查函数的命名空间是不明智的。某种类似于b $ b束状物体似乎是最令人满意的方式。


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


Probably because values are then required for those arguments. Plus it''s
a lot of work to specify "a very long list", and the list will also need
maintaining.

I must, however, agree with Mike''s advice: it''s unwise to try and
pollute a function''s namespace with arbitrary variables. Some kind of
bunch-like object would seem to be the most satisfactory way to go.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


这篇关于** kwargs自动绑定到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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