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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:02:21+00:00 2026-05-15T17:02:21+00:00

I am calling a constructor in ClassA and want to have the resulting object

  • 0

I am calling a constructor in ClassA and want to have the resulting object be of a different class (ClassB) if a certain condition is met. I’ve tried replacing the first argument to __init__() (‘self’ in the example below) within __init__() but it doesn’t seem to do what I want.

in main:

import ClassA

my_obj = ClassA.ClassA(500)
# unfortunately, my_obj is a ClassA, but I want a ClassB!

in ClassA/__init__.py:

import ClassB

class ClassA:
    def __init__(self,theirnumber):
        if(theirnumber > 10):
            # all big numbers should be ClassB objects:
            self = ClassB.ClassB(theirnumber)
            return
        else:
            # numbers under 10 are ok in ClassA.
            return

in ClassB/__init__.py:

class ClassB:
    pass
  • 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-15T17:02:21+00:00Added an answer on May 15, 2026 at 5:02 pm

    You need __new__() for that. (And you also need to make it a new-style class, assuming you’re using Python 2, by subclassing object.)

    class ClassA(object):
        def __new__(cls,theirnumber):
            if theirnumber > 10:
                # all big numbers should be ClassB objects:
                return ClassB.ClassB(theirnumber)
            else:
                # numbers under 10 are ok in ClassA.
                return super(ClassA, cls).__new__(cls, theirnumber)
    

    __new__() runs as part of the class instantiation process before __init__(). Basically __new__() is what actually creates the new instance, and __init__() is then called to initialize its properties. That’s why you can use __new__() but not __init__() to alter the type of object created: once __init__() starts, the object has already been created and it’s too late to change its type. (Well… not really, but that gets into very arcane Python black magic.) See the documentation.

    In this case, though, I’d say a factory function is more appropriate, something like

    def thingy(theirnumber):
        if theirnumber > 10:
            return ClassB.ClassB(theirnumber)
        else:
            return ClassA.ClassA(theirnumber)
    

    By the way, note that if you do what I did with __new__() above, if a ClassB is returned, the __init__() method you wrote in ClassA will not be called on the ClassB instance! When you construct the ClassB instance inside ClassA.__new__(), its own __init__() method (ClassB.__init__()) will be run at that point, so hopefully it makes sense that Python shouldn’t run another unrelated __init__() method on the same object. But this can get kind of confusing, which is probably another argument in favor of the factory function approach.

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

Sidebar

Related Questions

How is object of serialized class created dynamically without calling the constructor when de-serialization
I have a class with a template constructor, and the code is actually calling
I have a class with internal constructor and want to Resolve it from Unity
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have
Possible Duplicate: Calling virtual method in base class constructor Calling virtual functions inside constructors
When you instantiate a new object by calling constructor, i.e. Foo bar = new
I have two classes: abstract Entity , abstract ClassA extends Entity , ClassB extends
My situation: I have a class that I want to use in a number
What's the best way to call a class constructor that can have lots of
i have an example descendant of TBitmap : TMyBitmap = class(TBitmap) public constructor Create;

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.