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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:35:49+00:00 2026-06-18T08:35:49+00:00

I am trying to apply the Singleton design pattern in the below code class

  • 0

I am trying to apply the Singleton design pattern in the below code

class SMSMgr( object ):
    _instance = None
    def __init__(self):
            self._allsp = []
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(SMSMgr, cls).__new__(
                                cls, *args, **kwargs)
        return cls._instance

    def loadsettings(self):
        get_all_sp = ServiceProvider.objects.filter(status = False)
        for obj in get_all_sp:
            cla = obj.class_Name
            a=globals()[str(obj.class_Name)](obj.userName,obj.password,obj.sendingurl)
            self._allsp.append(a)
            #print self._allsp
    def send(self):
        print "+++++++++++++++++++== Global send "


if __name__ == "__main__":

    b = SMSMgr()
    b.loadsettings()
    print b._allsp
    print "b end -------------------"
    c = SMSMgr()
    c.loadsettings()
    print c._allsp
    print "c end -------------------"

According to singleton b object should be same as c object .
But when i am running this above code i am getting the different objects for b and c
Please suggest me how can i do this as a Singleton.

  • 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-18T08:35:50+00:00Added an answer on June 18, 2026 at 8:35 am

    I’m pretty sure the problem here is that there is no sentinel for your __init__, so even though each instantiation returns the same object, __init__ is being rerun each time as well. You could try something like this:

    def __init__(self):
        if not hasattr(self, '_allsp'):
            self._allsp = []
    

    Though for a more robust solution, you probably want something more like:

    def __init__(self):
        if not hasattr(self.__class__, 'inited'):
            self._allsp = []
            self.__class__.inited = True
    

    Just be aware that this is not thread safe unless you wrap the init functionality in a lock of some sort.

    As @mgilson eludes to in his answer though, if you are trying to create a singleton in Python, you probably should just be using class attributes instead of instance attributes, and looking at whether it even makes sense for the class to be completely separate to begin with, or to fold it in to some other, non-singleton class. There’s also a pattern, the borg pattern, that is frequently mentioned when singetons and Python are brought up in the same paragraph.

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

Sidebar

Related Questions

im trying to apply this JSON code to my Google Map: [ { stylers:
I'm trying to apply border-radius effect on a particular div. The code I used
i'm trying to apply the MVVM pattern in my application with MVVM Light. I've
Im trying to apply the state pattern on a multi threaded application.The problem is
I am trying to apply css class style to my @Html.TextBoxFor control in mvc3
I'm trying to apply a class to a div only when the window top
Iam trying to apply onTouchListener but iam running into few code problems,without switchcase it
I'm trying to apply a texture to a vertex array whit the following code:
I'm trying to apply a css class to all <th class='euroth'></th> which are located
While trying to apply policy-based design, I got stuck on this one (simplified): template

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.