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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:33:29+00:00 2026-05-20T14:33:29+00:00

my first question here: I’ve searched the internet and also read several question and

  • 0

my first question here:

I’ve searched the internet and also read several question and answers here before and finally figured out the way of writing the singleton classes for my python codes.
I’ve also read the python documentation about the new() function and some other things but still confused about how and what is mean by all that new(cls, *args, **kw) things etc!

for example I wrote a test code like this:

class Singleton(object):

    def __new__(cls, *args, **kwargs):
        if '_inst' not in vars(cls):
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
        return cls._instance

class printer(Singleton):

    def __init__(self):

        print "I am a new Object, and will remain until the end of time!"

if __name__ == '__main__':
    printer()

and the result is the text “I am a new Object, and will remain until the end of time!”
But how this works, I mean, I don’t know how to tell, for example I’m really confused about:

vars(cls): in the line if '_inst' not in vars(cls)

from where the vars(cls) comes out, I didn’t declare that before!
Can someone please clarify this things in the singleton class for me plus some about the last line

if __name__ == '__main__':
    printer()
  • 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-20T14:33:29+00:00Added an answer on May 20, 2026 at 2:33 pm

    First of all, this is wrong:

    class Singleton(object):
        def __new__(cls, *args, **kwargs):
            if '_inst' not in vars(cls): # <-- this must of course also be "_instance"
                cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
            return cls._instance
    

    An explicit __new__ method can be used like the factory pattern, so when you write printer(), Python will first call __new__, passing the class type and the constructor parameters (in this example none).

    '_instance' not in vars(cls) just means to look up whether the class already has an attribute called “_instance” (i.e. the singleton). If not, one is created. The trick with vars (builtin function, to answer your question) is not necessary, one could also use cls._instance and catch AttributeError, for example.

    And the last thing:

    if __name__ == '__main__':
        printer()
    

    This just creates a printer instance (using Singleton.__new__) if the script is executed directly (i.e. if __name__ == '__main__', not if the module is imported).

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

Sidebar

Related Questions

first question here. I'm developing a program in C# (.NET 3.5) that displays files
this is my first question here so I hope I can articulate it well
Greetings to all! This is my first question here on stackoverflow. I have a
First question on here so please be nice :) I know very little about
Here's my first question at SO. I have a internal application for my company
this is my first question to stackoverflow so here it goes... I use cruise
Simple question, is there any way to omit the double quote in PostgreSQL? Here
This is my very first question so I am a bit nervous about it
This is my first question so please be patient :) Background: I'm implementing an
First shot at throwing a question on these boards so hopefully I can get

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.