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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:09:05+00:00 2026-06-14T03:09:05+00:00

Given two modules, main and x with the following contents: main: class Singleton(object): _instance

  • 0

Given two modules, main and x with the following contents:

main:

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        cls._instance.x = 10
        return cls._instance
uvw = Singleton()

if __name__ == "__main__":
    print(id(uvw))
    uvw.x += 10
    print(uvw.x)
    import x

and x, respectively:

import main

print(id(main.uvw))
print(main.uvw.x)

I would now expect that executing main would yield the same IDs and the value twenty in both instances, but what I get is this:

$ python main.py
140592861777168
20
140592861207504
10

Is there any way I can ensure that uvw is the same object at both places?

  • 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-14T03:09:06+00:00Added an answer on June 14, 2026 at 3:09 am

    Python loads each module by name once (unless reload(module) is called). If you run main.py, the module is __main__ (try printing uvw.__class__.__module__). When x imports main, the module called main is being loaded for the first time.

    If you defined uvw in a third module, or in x — as long as it is imported in the same way into __main__ and x — it would be the same object.

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

Sidebar

Related Questions

Given two methods on the same class in Java : public void doSomething( Person
I'm wondering what the difference is between the following two modules # First Example
I have two files, one of the test.py is import new.py class Test: def
Given two data frames: C1<-c(3,4,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,6,3,7,4) DF<-data.frame(C1=C1,C2=C2,C3=C3) DF C1 C2 C3 1 3
Given two tuples of the same arity, how can I lexicographically compare them? It
Given two arrays, how to find the maximum element which is common to both
Given two sorted arrays of integers, a and b , and an integer c
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
Given two identical boost::variant instances a and b , the expression ( a ==
Given: two images of the same subject matter; the images have the same resolution,

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.