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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:26:08+00:00 2026-06-10T13:26:08+00:00

I have some few classes which are widespread in my Python application and which

  • 0

I have some few classes which are widespread in my Python application and which should have only one global instance (eg Logger, DbConnection). Python does not support static variables/methods in a class, so the usual Java/C++ way to create a singleton here does not work. I have looked for alternatives to implement singleton in Python. I want a simple (no metaprogramming if possible) and clean implementation. This looks good:

class MyClass(object):
    def a():
        pass

singleton = MyClass()

Using the singleton would be simple as

import myClass
myClass.singleton.a()

The direct assignment could be replaced by a creation function if object initialization is not so simple.

I could also create a getInstance() in module scope and always use it to get myObj.

Question 1) This works ok? The module code (myObj assignment) only runs the first time it is imported into some other module and myObj won’t be created every time I import this module somewhere?

An alternative method I have seen is to use a globals module. Something like:

from myClass1 import MyClass1
from myClass2 import MyClass2

myObj1 = MyClass1()
myObj2 = MyClass2()

Using this:

import globals
globals.myObj1.a()

I tend to prefer the first alternative.

Question 2) Between the 2 solutions, what do you recommend?

Question 3) A third solution would be passing the widespread objects such as Logger to several classes/functions, but this is not a good solution imho. Is there a better solution not mentioned here ?

I’m aware of the downsides of using global variables and singletons. However, having a global state is not a big issue in my application. I’ll prefer solutions that have code clarity and are simple to use.

  • 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-10T13:26:10+00:00Added an answer on June 10, 2026 at 1:26 pm

    If you want to have a logger class that only has one instance, just make it a separate module.

    # In logging.py
    def log(msg):
      print msg
    

    Then from any script you want logging in.

    from logging import log
    log("A critical error occured.")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some python modules containing mostly functions and a few classes. Each one
I have a c# application / solution ( only few classes and files) which
I have a few singleton classes with some extra functions which are run in
I have a few computationally expensive properties on some classes. These properties do not
I need to create module in Magento which will have few database tables. One
I have a web application which converts some PCL files to PDF by running
I have a few classes which do nothing except in their constructors/destructors. Here's an
I have some methods that are common across a few of my domain classes.
I have been researching and have some few ideas about a distributed caching system
For a few days a have some problem. I need to show simple ProgressBar

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.