4

我有以下类,用于存储要访问的其他对象的设置:

#!/usr/bin/env python                                                           

class Settings:                                                                    
    """Singleton settings class"""                                              

    settings = {                                                                   
                   'SectionA': {'a': 1, 'b': 2, 'c': 3},                           
                   'SectionB': {'d': 4, 'e': 5, 'f': 6}                            
               }                                                                   

    def __getitem__(self, key): return self.settings[key]                          
    def __setitem__(self, key, item): self.settings[key] = item                    
    def keys(self): return self.settings.keys()                                    

s = Settings()                                                                     
print s.keys()                                                                     
print s['SectionA']                                                                
print s['SectionB']                                                                

for key in s: print s[key]    

我不明白为什么它会产生以下输出或如何解决它,非常感谢任何帮助:

foo@bar:~$ python Settings.py
['SectionB', 'SectionA']
{'a': 1, 'c': 3, 'b': 2}
{'e': 5, 'd': 4, 'f': 6}
Traceback (most recent call last):
  File "Settings.py", line 20, in <module>
    for key in s: print s
  File "Settings.py", line 11, in __getitem__
    def __getitem__(self, key): return self.settings[key]
KeyError: 0
foo@bar:~$
4

2 回答 2

4

那是因为当您迭代 Settings 类时,它显然会尝试按索引对其进行迭代,就像一个列表一样。第一个索引是0,它给出一个 KeyError ,因为它不在您的字典中。您必须定义特殊方法来覆盖for key in s语法以遍历您的键。

def __iter__(self): return iter(self.settings)

编辑:另一件事是您正在使用已弃用的旧样式类。你应该把它改成Settings(object)

于 2012-08-07T23:45:00.997 回答
2

这有效:

class Settings(dict):                                                                    
    """Singleton settings class"""                                              

    settings = {                                                                   
                   'SectionA': {'a': 1, 'b': 2, 'c': 3},                           
                   'SectionB': {'d': 4, 'e': 5, 'f': 6}                            
               }                                                                   

    def __getitem__(self, key): return self.settings[key]                          
    def __setitem__(self, key, item): self.settings[key] = item                    
    def keys(self): return self.settings.keys()                                    

s = Settings()                                                                     

for key in s.keys(): 
    print key 
    print s[key]

印刷:

SectionB
{'e': 5, 'd': 4, 'f': 6}
SectionA
{'a': 1, 'c': 3, 'b': 2}

编辑

看,不{}.keys()

class Settings(dict):                                                                    
    """Singleton settings class"""                                              

    settings = {                                                                   
                   'SectionA': {'a': 1, 'b': 2, 'c': 3},                           
                   'SectionB': {'d': 4, 'e': 5, 'f': 6}                            
               }                                                                   

    def __getitem__(self, key): return self.settings[key]                          
    def __setitem__(self, key, item): self.settings[key] = item  
    def __iter__(self): return iter(self.settings.keys())               
    def keys(self): return self.settings.keys()      


s = Settings()                                                                                                                                   

for key in s: 
    print key 
    print s[key]

打印相同的输出...

于 2012-08-07T23:50:03.867 回答