Re:在字典中存储引用而不是副本 [英] Re: storing references instead of copies in a dictionary
问题描述
Calvin Spealman写道:
对于你的实际问题......你为什么还要这样做呢?如果你想改变字典中的函数
,为什么不简单地定义你想要使用的函数,并改变你想要使用的函数当你想改变它时,你必然会在字典中输入关键字吗?换句话说,
一次定义它们,然后只是d [''1''] = new_f1。什么是错的
那个?
好吧,除了我需要记住以外我什么也没做。
假设一个人经常这样做程序。它变得单调乏味。我想b $ b我想我会定义一些辅助函数:
>> def helper(fundict,newfun):
.... fundict [newfun.func_name] = newfun
....
_If_有一些更短但仍然正确的这样做的方法,我会使用
吧。如果没有,没什么大不了的。
完整性:
def new_f1(arg):
返回" NEW f1" + arg
f1.func_code = new_f1.func_code
不要使用它,除非你真的需要,我几乎承诺你不要。
我保证我不会用它。 :-)这似乎是''错误的事情''。
mk写道:
Calvin Spealman写道:
>对于你的实际问题......你为什么还要这样做呢?如果你想改变词典中的功能,为什么不简单地定义你想要使用的功能,并改变你必须使用的功能
当你想改变它时字典中的键?换句话说,
一次定义它们,然后只是d [''1''] = new_f1。有什么问题呢?
好吧,除了我需要记住我必须这样做之外什么都没有。
假设一个人经常这样做程序。它变得单调乏味。我想b $ b我想我会定义一些辅助函数:
>> def helper(fundict) ,newfun):
... fundict [newfun.func_name] = newfun
...
_If_有一些更短但仍然正确的这样做的方法,我会使用
吧。如果不是,没什么大不了的。
>完整性:
def new_f1(arg):
返回" NEW f1 + arg
f1.func_code = new_f1.func_code
不要使用它,除非你真的需要,我几乎承诺你不会。
我保证我不会用它。 :-)这似乎是一个错误的事情。
嗯,它可能完全是非pythonic但这可以做你想要的:
def f2(arg):
返回f2+ arg
def f1(arg):
返回" f1" + arg
a = {" 1":" f1"," ; 2":" f2"}
打印[eval(x [1])(x [0])for a in aite()]
def f2(arg):
返回新f2+ arg
打印[eval(x [1])(x [0])x中的x。 item()]
不知道这对你有用..
< blockquote class =post_quotes>
def f2(arg):
return" f2" + arg
def f1(arg) :
返回" f1" + arg
a = {" 1":" f1"," 2":" f2" }
打印[eval(x [1])(x [0])for a in aite()]
def f2(arg):
返回"新f2" + arg
打印[eval(x [1])(x [0])for a in aite()]
干净的技巧,如果在某些情况下可能很危险。无论如何,谢谢,
我没想到。
不知道这对你有用。 。
至少我学到了一些东西。 :-)
7月17日上午10:05 * am,mk< mrk ... @ gmail.comwrote:
def f2(arg):
* * return" f2" + arg
def f1(arg):
* * return" f1" + arg
a = {" 1":" f1"," 2":" f2"}
print [eval(x [1])( x [0])对于a.items中的x()]
def f2(arg):
* * return" New f2" + arg
打印[eval(x [1])(x [0])for a in aite()]
干净的技巧,如果可能的话在某些情况下很危险。无论如何,谢谢,
我没想到。
不知道这对你有用。 。
至少我学到了一些东西。 :-)
您希望对变化的变量进行一致的访问。将其包装在
对象中:
>> a =空白()
a.ref =''X''
a.ref
' 'X''
>> b = a
b.ref
''X''
< blockquote class =post_quotes>
>> a.ref =''Y''
b.ref
''Y''
>>>
Calvin Spealman wrote:
To your actual problem... Why do you wanna do this anyway? If you want
to change the function in the dictionary, why don''t you simply define
the functions you''ll want to use, and change the one you have bound to
the key in the dictionary when you want to change it? In other words,
define them all at once, and then just d[''1''] = new_f1. What is wrong
with that?
Well, basically nothing except I need to remember I have to do that.
Suppose one does that frequently in a program. It becomes tedious. I
think I will define some helper function then:
>>def helper(fundict, newfun):
.... fundict[newfun.func_name] = newfun
....
_If_ there were some shorter and still "proper" way to do it, I''d use
it. If not, no big deal.
For completeness:
def new_f1(arg):
return "NEW f1 " + arg
f1.func_code = new_f1.func_code
Don''t use that unless you really have to and I nearly promise that you don''t.
I promise I won''t use it. :-) It seems like a ''wrong thing to do''.
mk wrote:Calvin Spealman wrote:>To your actual problem... Why do you wanna do this anyway? If you want
to change the function in the dictionary, why don''t you simply define
the functions you''ll want to use, and change the one you have bound to
the key in the dictionary when you want to change it? In other words,
define them all at once, and then just d[''1''] = new_f1. What is wrong
with that?
Well, basically nothing except I need to remember I have to do that.
Suppose one does that frequently in a program. It becomes tedious. I
think I will define some helper function then:
>>def helper(fundict, newfun):
... fundict[newfun.func_name] = newfun
...
_If_ there were some shorter and still "proper" way to do it, I''d use
it. If not, no big deal.
>For completeness:
def new_f1(arg):
return "NEW f1 " + arg
f1.func_code = new_f1.func_code
Don''t use that unless you really have to and I nearly promise that you
don''t.
I promise I won''t use it. :-) It seems like a ''wrong thing to do''.
Well it''s probably totally "non pythonic" but this does what you want:
def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Don''t know if this is any use to you..
def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn''t think of that.
Don''t know if this is any use to you..At least I learned something. :-)
On Jul 17, 10:05*am, mk <mrk...@gmail.comwrote:def f2(arg):
* * return "f2 "+arg
def f1(arg):
* * return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
* * return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn''t think of that.
Don''t know if this is any use to you..
At least I learned something. :-)You want consistent access to a changing variable. Wrap it in an
object:
>>a= Blank( )
a.ref= ''X''
a.ref
''X''
>>b= a
b.ref
''X''
>>a.ref= ''Y''
b.ref
''Y''
>>>
这篇关于Re:在字典中存储引用而不是副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!