更改函数的本地命名空间 [英] changing local namespace of a function

查看:84
本文介绍了更改函数的本地命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的清单,


我有很多字典都有相同的密钥组,我想

写一个函数来计算基于这些的东西值。对于

的例子,我有


a = {''x'':1,'y'':2}

b = {''x'':3,''y'':3}


def fun(dict):

dict [''z ''] = dict [''x''] + dict [''y'']


fun(a)和fun(b)会将每个字典中的z设置为x和y的总和。


我的函数和字典比这些复杂得多,所以我想要将bict设置为默认的命名空间。这是

可能吗?理想的代码是:


def fun(dict):

#set dict as local namespace

#locals() = dict?

z = x + y


非常感谢提前。

Bo

Dear list,

I have many dictionaries with the same set of keys and I would like to
write a function to calculate something based on these values. For
example, I have

a = {''x'':1, ''y'':2}
b = {''x'':3, ''y'':3}

def fun(dict):
dict[''z''] = dict[''x''] + dict[''y'']

fun(a) and fun(b) will set z in each dictionary as the sum of x and y.

My function and dictionaries are a lot more complicated than these so I
would like to set dict as the default namespace of fun. Is this
possible? The ideal code would be:

def fun(dict):
# set dict as local namespace
# locals() = dict?
z = x + y

Many thanks in advance.
Bo

推荐答案

你好博,

不要使用dict它是内置的;)


也试试这个在解释器会话中,它很简单快速

来获得你自己的答案。
Hello Bo,
Don''t use dict it is a builtin ;)

Also try this stuff out in an interpreter session it is easy and fast
to get your own answers.
def fun(d):
def fun(d):



.... __ dict__ = d

.... return __dict__

hth,

MEFarmer


.... __dict__ = d
.... return __dict__
hth,
M.E.Farmer


Bo Peng写道:
Bo Peng wrote:
亲爱的清单,

我有很多字典都有相同的键,我想写一个函数来计算基于这些值的东西。例如,我有

a = {''x'':1,''y'':2}
b = {''x'':3 ,'y'':3}

def fun(dict):
dict [''z''] = dict [''x''] + dict [''y '']

有趣(a)和有趣(b)将每个词典中的z设置为x和y的总和。

我的功能和词典更多比这些复杂,所以我想将dict设置为有趣的默认命名空间。这可能吗?理想的代码是:

def fun(dict):
#set dict as local namespace
#locals()= dict?
z = x + y
Dear list,

I have many dictionaries with the same set of keys and I would like to
write a function to calculate something based on these values. For
example, I have

a = {''x'':1, ''y'':2}
b = {''x'':3, ''y'':3}

def fun(dict):
dict[''z''] = dict[''x''] + dict[''y'']

fun(a) and fun(b) will set z in each dictionary as the sum of x and y.

My function and dictionaries are a lot more complicated than these so I
would like to set dict as the default namespace of fun. Is this
possible? The ideal code would be:

def fun(dict):
# set dict as local namespace
# locals() = dict?
z = x + y



正如你无疑从文档和这个小组中发现的那样,这对CPython来说是不可行的。




如果你必须把你的函数写成真正的函数,那么你可能会做这样的事情



As you no doubt have discovered from the docs and this group, that isn''t doable
with CPython.

If you must write your functions as real functions, then you might do something
like this:

a = {''x'':1,''y'':2}
b = {''x'':3,''y'':3}
... def funa(x,y,** kw):
... del kw#使用这种方法在当地人中担心不需要的名字

... z = x + y

...返回当地人()

... a.update(funa(** a))
b.update(funa (** b))
a
{''y'':2,''x'':1,''z'':3} b
{''y' ':3,''x'':3,''z'':6}



另外,你可以使用exec:

a = {''x'':1,''y'':2}
b = {''x'':3,''y'':3}
exec" z = x + y"在globals()中,a
{''y'':2,''x'':1,''z'':3} exec" z = x + y"在globals()中,b
b
{''y'':3,''x'':3,''z'':6}
a = {''x'':1, ''y'':2}
b = {''x'':3, ''y'':3} ... def funa(x,y, **kw): ... del kw #Careful of unwanted names in locals with this approach
... z = x + y
... return locals()
... a.update(funa(**a))
b.update(funa(**b))
a {''y'': 2, ''x'': 1, ''z'': 3} b {''y'': 3, ''x'': 3, ''z'': 6}

Alternatively, you could use exec:
a = {''x'':1, ''y'':2}
b = {''x'':3, ''y'':3}
exec "z = x + y" in globals(), a
a {''y'': 2, ''x'': 1, ''z'': 3} exec "z = x + y" in globals(), b
b {''y'': 3, ''x'': 3, ''z'': 6}




Michael



Michael


Bo Peng写道:
Bo Peng wrote:
我的函数和字典比这些要复杂得多,所以我想把dict设置为有趣的默认命名空间。
My function and dictionaries are a lot more complicated than these so I
would like to set dict as the default namespace of fun.




这听起来像我一样你试图重新实现面向对象。


将所有这些函数转换为类的方法,而不是创建字典'
'将传递给函数,创建类

实例。


类MyClass(对象):

def __init __(self, ** kwargs):

for key,val in kwargs:

setattr(self,key,val)

def fun(self):

self.z = self.y + self.x


a = MyClass(x = 1,y = 2)

a.fun()

打印az


Jeff Sh annon

技术员/程序员

Credit International



This sounds to me like you''re trying to re-implement object orientation.

Turn all of those functions into methods on a class, and instead of
creating dictionaries that''ll be passed into functions, create class
instances.

class MyClass(object):
def __init__(self, **kwargs):
for key, val in kwargs:
setattr(self, key, val)
def fun(self):
self.z = self.y + self.x

a = MyClass(x=1, y=2)
a.fun()
print a.z

Jeff Shannon
Technician/Programmer
Credit International


这篇关于更改函数的本地命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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