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 8597607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:57:03+00:00 2026-06-12T00:57:03+00:00

Edit: As I just found out, Singleton isn’t that useful in python. python uses

  • 0

Edit: As I just found out, “Singleton” isn’t that useful in python. python uses “Borg” instead. http://wiki.python.de/Das%20Borg%20Pattern With Borg I was able to read & write global variables from different classes like:

b1 = Borg()
b1.colour = "red"
b2 = Borg()
b2.colour
>>> 'red'

But I was not able to create/read a list with borg like:

b1 = Borg()
b1.colours = ["red", "green", "blue"]
b2 = Borg()
b2.colours[0]

Is this something Borg doesn’t support? If yes: How can I create global lists which I can read & write from different classes?


Original Question:

I want to read & write global variables from different classes. Pseudocode:

class myvariables():
    x = 1
    y = 2

class class1():
    # receive x and y from class myvariables
    x = x*100
    y = y*10
    # write x and y to class myvariables

class class2():
    # is called *after* class1
    # receive x and y from class myvariables
    print x
    print y

printresult should be “100” and “20”.
I’ve heard that “Singleton” can do this… but I didn’t found any good explanation of “Singleton”. How can I make this simple code work?

  • 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-12T00:57:04+00:00Added an answer on June 12, 2026 at 12:57 am

    Borg pattern class attrs will not be reset on new instance calls, but instance attrs will be. Be sure that you are using class attrs instead of instance attrs if you want to preserve previously set values. The code below will do what you want.

    class glx(object):
        '''Borg pattern singleton, used to pass around refs to objs. Class
        attrs will NOT be reset on new instance calls (instance attrs will).
        '''
        x = ''
        __sharedState = {}
        def __init__(self):
            self.__dict__ = self.__sharedState
            #will be reset on new instance 
            self.y = ''  
    
    
    if __name__ == '__main__':
        gl = glx()
        gl.x = ['red', 'green', 'blue']
        gl2 = glx()
        print gl2.x[0]
    

    To prove the point, try it again with the instance attr y. You will get an unhappy result.

    good luck,
    Mike

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

Sidebar

Related Questions

Edit: to clarify - i just found out that it is really easy to
I have MacPorts and CPAN installed and I've only just found out that it's
I just found out that the Screen Capture by Google extension makes my website's
Edit: Just found out this is a chrome problem, the code works fine in
i just found out that Chrome is ignoring CSS min-width: ...; . I can't
EDIT I've just started skimming Codd's famous 1970 paper that started it all, that
Edit: Turns out I just had some values -switched-. I'm working with OpenGL and
EDIT 2: I have just realised that my HTML is only displaying the artist
Edit For simplicity: I just want to make the most basic possible cursor that
Just a little edit, in case it helps anyone out who happens to stumble

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.