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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:45:42+00:00 2026-06-13T07:45:42+00:00

I tried to return a value in a class constructor ( init ): class

  • 0

I tried to return a value in a class constructor (init):

class A:
  def __init__(self):
    return 1

but there is a run-time error saying init should return None. If this is the case, how to understand:

a=A()

where “a” is assigned as the class instance?

  • 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-13T07:45:43+00:00Added an answer on June 13, 2026 at 7:45 am

    Strictly speaking, it’s not A.__new__() that’s creating the the instance a.

    When you define class A(object): (or class A: as well if you are using Python3, class A: is the old-style class that has been deprecated), it is __new__ from the inheritedobject.__new__() that is being called to create the instance a.

    When a = A() is executed, what happens is:

    1. A() is a shorthand for A.__call__
    2. object.__new__(cls, *args, **kwargs) where cls=A, is what actually happens under hood to create instance a. It allocates the memory for the new object, and should then return a new object (the instance).
    3. if and only if the newly created object is returned does __init__(self) then get called with the newly created object passed to it to “initilize” the object.

    Consider the following demo:

    • when we override __new__ and no longer returns an object, __init__ will
      not get called:

      class A(object):
      
          def __new__(cls, *args, **kwargs):
              print cls, args, kwargs
      
          def __init__(self):
              self.x = 'init!'
              print self.x
      
      
      In : a = A()
      <class '__main__.A'> () {}
      
      # note that "init!" has not appeared here because __new__ didn't return an
      # new instance
      
    • now, return a new instance by using object.__new__, and you will see that
      after __new__, __init__ would be called as well:

      class A(object):
      
          def __new__(cls, *args, **kwargs):
              print cls, args, kwargs
              return object.__new__(cls, args, kwargs)
      
          def __init__(self):
              self.x = 'init!'
              print self.x
      
      In : a = A()
      <class '__main__.A'> () {}
      init!
      

    Here is another demo to display the difference, note that instance a can be created without calling __init__():

    class A(object):
        def __init__(self):
            self.x = "init!"
            print self.x
    
    In : a = object.__new__(A)
    
    In : a
    Out: <__main__.A at 0x103466450>
    
    In : a.__dict__
    Out: {}
    
    
    In : aa = A()
    init!
    
    In : aa
    Out: <__main__.A at 0x1033ddf50>
    
    In : aa.__dict__
    Out: {'x': 'init!'}
    

    Now for the inquisitive (and also to refresh my own memory =]):

    Roughly speaking, there are two main ways to create new objects in Python:

    Create new object (type / class) by subclassing:

    class statements tells Python to create a new type / class object(by
    subclassing an existing type/class such as object):

    class Hello(object):
        pass
    >>> Hello.__class__
    <type 'type'>
    

    In fact all class/type object have type type. The type of type
    (type(type)) is still type.

    You can subclass a type / class object.

    Create new object (instance) by instantiating:

    You can also create a new object by instatiating an existing type object.
    This is done via the using the __call__ operator (shorthand by ()):

    >>> h = hello()
    >>> type(h)
    <class '__main__.Hello'>
    >>> type(int('1'))
    <type 'int'>
    

    You cannot subclass an instance object.

    (note that you can also create a new instance object by some other means such
    as using the list operator [1,2,3], in this case it creates an list instance)

    You can check an object’s type by type(my_obj) or my_object.__class__.


    Now you know how an instance object is created, but what really creates the type / class object (that allows to create instance objects)?

    In fact, these objects are created by instantiation as well, albeit it is a
    slightly different kind of instantiation from what was mentioned earlier.

    Aside from class statement, you can also use
    type(cls_name, parent_class_tuple, attr_dict) to create a new class.
    For eg:

    type('Hello', (object,), {})
    

    will create the Hello class same as the one shown earlier.

    What is type? Enter metaclass.

    type is a metaclass, which is the class of class, i.e., classes are
    instances of metaclasses. The __class__ of type is still type.

    So here is a graph that shows the relationships between metaclass, class,
    instance:

                instantiate             instantiate
    metaclass   --------------> class   ---------------->    instance
                type.__new__()          object.__new__()
    

    When metaclass type is called to create a new class, the similar flow goes:

    1. type.__call__() is excuted
    2. type.__new__() allocates memory and then returns new a class (a metaclass instace), and then calls type.__init__().
    3. type.__init__() initalizes the newly created class that was passed from step 2.

    You can even create a new metaclass by subclassing type:

    class MyMeta(type):
        def __new__(meta, name, bases, dct):
            # do something
            return super(MyMeta, meta).__new__(meta, name, bases, dct)
        def __init__(cls, name, bases, dct):
            # do something
            super(MyMeta, cls).__init__(name, bases, dct)
    

    then you can create a new class from this MyMeta metaclass just like you do
    with type:

    MyClass = MyMeta('MyClass', (object, ), {'x': 1})
    

    Or, use __metaclass__ when defining your class, which has exactly the
    same effect as what was shown above:

    class MyClass(object):
        __metaclass__ = MyMeta
        x = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
XmlHttpRequest works through callbacks. So how can I return a value? I tried to
I tried to run this example but I got binding problem. Designer highlights the
Is is possible to return two values from a WebService to jQuery. I tried
I tried this as a simple test: @functions { private MvcHtmlString helloWorld() { return
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried a bunch of things but I can't get it to work consistently amid
I have a class FileTransfer that uses an ofstream object, log. The class' constructor
Consider the following code: class Program { static Program() { Program.program1.Value = 5; }

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.