Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:20:54+00:00 2026-06-09T06:20:54+00:00

I have the following class which I use to store settings for other objects

  • 0

I have the following class which I use to store settings for other objects to access:

#!/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]    

I can’t understand why it produces the following output or how to get around it, any help greatly appreciated:

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:~$
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:20:56+00:00Added an answer on June 9, 2026 at 6:20 am

    This works:

    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]
    

    Prints:

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

    Edit

    Look ma, no {}.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]
    

    Prints same output…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class which handles my user logged in / logged out
I have the following asynctask class which is not inside the activity. In the
I have the following javascript class which I am trying to pass to an
In Java I have created the following class which extends ImageView to create a
I have the following tricky problem: I have implemented a (rather complicated) class which
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
I have a class which must implement the following property public ICollection<IType> Items {
I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,
I have following situation - Have a MongoService class, which reads host, port, database
I have a DirectoryMonitor class which works on another thread. It has the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.