Python 原子 API : how to set atom var when using a dictionary

标签 python

Atom api是 Enaml 用来实现 MVC 的库。更改原子变量,UI 会更新。在 UI 中更改它,您的模型就会更新。

我想将一个 Atom var(在本例中为 Bool())放入字典中,稍后更新该 var

 from atom.api import Atom,Bool
 class MyModel(Atom):
     myBool = Bool()

     def getDict(self):
         return {'mybool':self.myBool}

     def setAllBoolsTrue(self):
         self.myBool = True #example to show that just setting mybool will update UI components that use it

         #now to show how I'd like to generalize to many components

         for k,v in self.getDict().iteritems():
             v = True  # this fails, even though the id(v) is the same as id(self.mybool)

最后一条语句没有更新mybool,它只是做了一个简单的赋值。

那么有没有一种方法可以更新从字典中检索到的 Bool(),就像简单地设置它一样?

编辑:代码已更新,因此没有语法错误。

编辑:根据评论中的建议,我试了没有成功:

    tempDict = self.getDict();
    #self.myBool = True  # this works
    tempDict['mybool'] = True  #this does not work

最佳答案

for k, v in getDict(): 将不起作用,除非您的 getDict() 函数返回两个键的字典,即 k, v 在这种情况下更恰本地命名为 key1, key2,其中 key2 甚至不存在。


如果你真的想实现类,你可以做类似...

class MyModel(Atom):

    def __init__(self):
        self.myBool = True


>>> model = MyModel()
>>> model.myBool
True
>>> model.myBool = False
>>> model.myBool
False

关于Python 原子 API : how to set atom var when using a dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253798/

相关文章:

python - 按 DataFrame 的另一列移动列分组的值

python - 如果超过最大值,则获取开始和停止之间的数字并重新启动计数器

iphone - CoreData sqlite3 db - 使用 python 脚本填充时间戳列

python - Jupyter 中存在 MemoryError,但 Python 中没有

python - 使用 Python 从 Google 文档中删除资源

python - Python数据解析中的正则表达式

python - 创建一个没有行替换的numpy矩阵

python - pymunk - 如何限制移动?

python - 定义 xrange 后的 StopIteration

python - 在 IPython 笔记本小部件和 Spyre 中动态更改下拉菜单