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

  • SEARCH
  • Home
  • 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 7018397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:02:44+00:00 2026-05-27T23:02:44+00:00

Why dictionary content shared between class instances, but strings not shared? Test code: class

  • 0

Why dictionary content shared between class instances, but strings not shared?

Test code:

class Config(object):
    file_names  = dict()
    file_name_1 = None
    file_name_2 = None

    def __init__(self):
        if not self.file_names:
            print "DEBUG: Config(): set initial values"
            self.file_names["0"] = None
            self.file_names["1"] = None

    def set_file_names(self, file_names):
        self.file_name_1     = file_names[0]
        self.file_names["0"] = file_names[0]
        self.file_name_2     = file_names[1]
        self.file_names["1"] = file_names[1]

class CommandLineParser(object):
    def __init__(self):
        self.cfg = Config()
        self.start()

    def start(self):
        self.cfg.set_file_names(("file_1.log", "file_2.log"))
        print self.__class__.__name__, "FileName1 (str) :", self.cfg.file_name_1
        print self.__class__.__name__, "FileName1 (dict):", self.cfg.file_names["0"]

class LogProcessor(object):
    def __init__(self):
        self.cfg   = Config()
        self.cmdln = CommandLineParser()
        print  self.__class__.__name__, "FileName1 (str) :", self.cfg.file_name_1
        print  self.__class__.__name__, "FileName1 (dict):", self.cfg.file_names["0"]

if __name__ == '__main__':
    processor = LogProcessor()

Output:

DEBUG: Config(): set initial values
CommandLineParser FileName1 (str) : file_1.log
CommandLineParser FileName1 (dict): file_1.log
LogProcessor FileName1 (str) : None
LogProcessor FileName1 (dict): file_1.log

Is it a right way to share options between instances in dictionary attribute?

  • 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-05-27T23:02:44+00:00Added an answer on May 27, 2026 at 11:02 pm

    files_names is a class attribute of your class Config so it is shared by all instances of this class. file_name_1 and file_name_2 are also initially class attributes (set to None) but you rebind them in instances inside the set_file_names method so they become instance attributes, private to each instance, at that time.

    Your idea to use a class attribute which is a mutable container (dict) to contain configuration common to all instances and which can be updated dynamically is good.

    If you still want to use the individual string attributes file_name_1 and file_name_2 but you want them to be shared between all instances, then you can do one of the following things:

    • Rebind the class attribute instead of creating an instance attribute in set_file_names by using Config.file_name_1 = whatever instead of self.file_name_1 = whatever
    • Make the set_file_names method a class method (use the @classmethod decorator) so that it will operate on the class, not on the instance.
    • Do not instantiate the Config class at all, single it is meant to be a singleton anyway. Use Config instead of Config().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My class contains a Dictionary<T, S> dict , and I want to expose a
Are there any dictionary classes in the .NET base class library which allow duplicate
I have a dictionary of 200,000 items (the keys are strings and the values
I have a dictionary where keys are strings, and values are integers. stats =
I am trying to render the dictionary content using Django template like this for
I have a silverlight class library (not a top-level application) with several user controls
Iam adding some text content to nsmutablestring in the form of dictionary format and
I'm going to use some content from an AppleScript dictionary server-side. I need to
I`m looking at printing the content of a dictionary into a table, the dictionary
I want to iterate to get into every content in following Dictionary. private Dictionary<Subject,

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.