0
什么是要实现这样的事情最好的办法:定义值有
dic = {
'foo': Tree(),
'foobar': #the method 'branch()' executed on 'foo'
}
可以这样做我描述的方式,或者我应该使用dic['foobar'] = dic['foo'].branch()
第二分配?
什么是要实现这样的事情最好的办法:定义值有
dic = {
'foo': Tree(),
'foobar': #the method 'branch()' executed on 'foo'
}
可以这样做我描述的方式,或者我应该使用dic['foobar'] = dic['foo'].branch()
第二分配?
我看不出有任何理由为什么不这样做喜欢:
t = Tree()
dic = {'foo': t, 'foobar': t.branch()}
但是,也许这一招可以帮助你:
dic = {'foo': Tree(), 'foobar': lambda: dic['foo'].branch()}
的缺点是,你必须使用dic['foobar']()
代替dic['foobar']
,并且每次都执行.branch()
函数,这可能不是您想要的。
有时最明显的答案是最好的答案。谢谢。 – Joren
如果字典中有3对,该怎么办?记住字典是无序的。 – Christian
@Wooble我编辑了我的问题,使我的意图更清楚。 – Joren