使用 Python 处理类字典

发布于 2024-09-13 06:51:14 字数 1111 浏览 10 评论 0原文

对于这个例子,我有一本字典,当我调用它时,会显示“Ember Attack”。

#import shelve
class Pokemon():
"""Each pokemon's attributes"""

def __init__(self):
    self.id=[]
    self.var1=[]
    self.var2=[]
    self.var3=[]
    self.var4=[]
    self.var5=[]

def __str__(self):
     showList=['id','var1', 'var2', 'var3', 'var4', 'var5']

#dict1=shelve.open("shelve.dat")
dict1={}
dict1["Charmander"]=Pokemon()
dict1["Charmander"].var1="Ember Attack"
#dict1.sync()
print dict1["Charmander"].var1
#dict1.close()

然而,当我开始使用书架而不是字典时,当我调用 var1 时,我得到一个空白。

import shelve

class Pokemon():
"""Each patient's attributes"""

def __init__(self):
    self.id=[]
    self.var1=[]
    self.var2=[]
    self.var3=[]
    self.var4=[]
    self.var5=[]

def __str__(self):
    showList=['id','var1', 'var2', 'var3', 'var4', 'var5']

dict1=shelve.open("shelve.dat")
#dict1={}

dict1["Charmander"]=Pokemon()
dict1["Charmander"].var1="Ember Attack"

dict1.sync()

print dict1["Charmander"].var1

dict1.close()

唯一的区别是我将 dict1 设为搁置字典而不是常规字典。它可能与内存范围或其他东西有关。无论如何,有人可以帮我修改我的代码,以便它可以与货架一起使用吗?谢谢!

For this example, I have a dictionary, that when I call on it, "Ember Attack" is displayed.

#import shelve
class Pokemon():
"""Each pokemon's attributes"""

def __init__(self):
    self.id=[]
    self.var1=[]
    self.var2=[]
    self.var3=[]
    self.var4=[]
    self.var5=[]

def __str__(self):
     showList=['id','var1', 'var2', 'var3', 'var4', 'var5']

#dict1=shelve.open("shelve.dat")
dict1={}
dict1["Charmander"]=Pokemon()
dict1["Charmander"].var1="Ember Attack"
#dict1.sync()
print dict1["Charmander"].var1
#dict1.close()

However when I start using shelves instead of the dictionary, I get a blank when I call on var1.

import shelve

class Pokemon():
"""Each patient's attributes"""

def __init__(self):
    self.id=[]
    self.var1=[]
    self.var2=[]
    self.var3=[]
    self.var4=[]
    self.var5=[]

def __str__(self):
    showList=['id','var1', 'var2', 'var3', 'var4', 'var5']

dict1=shelve.open("shelve.dat")
#dict1={}

dict1["Charmander"]=Pokemon()
dict1["Charmander"].var1="Ember Attack"

dict1.sync()

print dict1["Charmander"].var1

dict1.close()

The only difference is that I made dict1 a shelve dictionary instead of a regular dictionary. It probably has to do with memory scope or something. Anyway, can someone help me revise my code so that it will work with shelves? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-09-20 06:51:14
dict1=shelve.open("shelve.dat", writeback=True)

您还可以指定应该提高性能的协议

dict1=shelve.open("shelve.dat", protocol=2, writeback=True)

由于Python语义,一个架子
无法知道何时可变
持久字典条目是
修改的。默认修改对象
仅当分配给
架子(参见示例)。如果可选的
writeback参数设置为True,
所有访问的条目也被缓存
在内存中,并在sync()上写回
并关闭();这可以使它更方便
改变可变条目
持久字典,但是,如果有很多
条目被访问,它可以消耗
大量内存用于缓存,
并且可以进行关闭操作
非常慢,因为所有访问的条目
被写回(没有办法
确定哪些访问的条目是
可变的,也不知道哪些实际上是可变的
突变)。

dict1=shelve.open("shelve.dat", writeback=True)

you can also specify the protocol which should improve performance

dict1=shelve.open("shelve.dat", protocol=2, writeback=True)

Because of Python semantics, a shelf
cannot know when a mutable
persistent-dictionary entry is
modified. By default modified objects
are written only when assigned to the
shelf (see Example). If the optional
writeback parameter is set to True,
all entries accessed are also cached
in memory, and written back on sync()
and close(); this can make it handier
to mutate mutable entries in the
persistent dictionary, but, if many
entries are accessed, it can consume
vast amounts of memory for the cache,
and it can make the close operation
very slow since all accessed entries
are written back (there is no way to
determine which accessed entries are
mutable, nor which ones were actually
mutated).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文