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

The Archive Base Latest Questions

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

class Singleton(type): def __init__(self, *args, **kwargs): print ‘calling __init__ of Singleton class’, self print

  • 0
class Singleton(type):
    def __init__(self, *args, **kwargs):
        print 'calling __init__ of Singleton class', self
        print 'args: ', args
        print 'kwargs: ', kwargs
        super(Singleton, self).__init__(*args, **kwargs)
        self.__instance = None
    def __call__(self, *args, **kwargs):
        print 'running __call__ of Singleton', self
        print 'args: ', args
        print 'kwargs: ', kwargs, '\n\n'
        if self.__instance is None:
            self.__instance = super(Singleton, self).__call__(*args, **kwargs)
        return self.__instance

class A(object):
    __metaclass__ = Singleton
    def __init__(self,a):
        print 'in __init__ of A:  ', self
        self.a = a
        print 'self.a: ', self.a

a=A(10)
b=A(20)

I copied this code from Ben’s answer to the question Python's use of __new__ and __init__? and modified it a little. But, I am not aware of the flow. Although I understand from a higher level what this code is intended to do. But, internally how it works, I am not quite sure of.

I get the following output when running this code:-

calling __init__ of Singleton class <class '__main__.A'>
args:  ('A', (<type 'object'>,), {'__module__': '__main__', '__metaclass__': <class '__main__.Singleton'>, '__init__': <function __init__ at 0x01F9F7B0>})
kwargs:  {}
running __call__ of Singleton <class '__main__.A'>
args:  (10,)
kwargs:  {}


in __init__ of A:   <__main__.A object at 0x01FA7A10>
self.a:  10
running __call__ of Singleton <class '__main__.A'>
args:  (20,)
kwargs:  {}

I cant understand how the args and kwargs for __init__ and __call__ become different.
While using metaclasses, this link (What is a metaclass in Python?) has explained how to use __new__ and a function as a metaclass. But, I do not understand how __call__ is being used.

Can somebody explain the flow? By this, I mean, the precedence in which __new__, __call__, __init__ are called and who calls them?

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

    Your code doesn’t include any __new__, so little can be said about it.

    But you create a metaclass which is instantiated at the time class A is created. In other words, the class A is an object itself and as such an instance of its metaclass Singleton.

    So let’s look what happens:

    After A‘s environment is finished (its methods exist, its dict exists as well, …), the class gets created as an instance of the metaclass. Essentially, the call is

    A = Singleton('A', (object,), <the dict>)
    

    where <the dict> is the dict containing the class’s namespace (here: __module__, __metaclass__ and __init__).

    On this call to Singleton, calling super(Singleton, self).__call__(*args, **kwargs) results in calling the __new__ method which returns a new instance, on which .__init__ is called afterwards.

    That’s why this happens:

    calling __init__ of Singleton class <class '__main__.A'>
    args:  ('A', (<type 'object'>,), {'__module__': '__main__', '__metaclass__': <class '__main__.Singleton'>, '__init__': <function __init__ at 0x01F9F7B0>})
    kwargs:  {}
    

    After A is constructed, you use it by instantiating it:

    a = A(10)
    

    This calls A. A is an instance of Singleton, so Singleton.__call__ is invoked – with the effect you see:

    running __call__ of Singleton <class '__main__.A'>
    args:  (10,)
    kwargs:  {}
    

    Singleton.__call__ calls type.__call__, this calls A.__new__ and A.__init__:

    in __init__ of A:   <__main__.A object at 0x01FA7A10>
    self.a:  10
    

    Then you do

    b = A(20)
    

    which calls Singleton.__call__:

    running __call__ of Singleton <class '__main__.A'>
    args:  (20,)
    kwargs:  {}
    

    Here the super call is suppressed and the old object is returned.

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

Sidebar

Related Questions

I came to know that Grails service class are of Singleton type. For what
I'm learning Python and i've been trying to implement a Singleton-type class as a
I'm trying to create a global (singleton) class that can associate any type of
I have a class (singleton) and it contains a static Dictionary private static Dictionary<string,
So, classic simple Singleton realization is following: class Singleton { private: static Singleton* singleton;
Can singleton class be static?
Should a Singleton class be allowed to have children? Should we seal it? What
PHP PDO Singleton Class: <?php require_once('app/config.php'); // Require constants HOST, DATABASE, USER, PASSWORD /*
I have a singleton class for global access to config information. This singleton class
I have a singleton class which is being used throughout the app. I am

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.