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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:12:14+00:00 2026-06-04T15:12:14+00:00

Let’s assume we have a class ‘Parent’ , that for some reason has __new__

  • 0

Let’s assume we have a class ‘Parent’ , that for some reason has __new__ defined and a class ‘Child’ that inherits from it.
(In my case I’m trying to inherit from a 3rd party class that I cannot modify)

class Parent:
    def __new__(cls, arg):
        # ... something important is done here with arg

My attempt was:

class Child(Parent):
    def __init__(self, myArg, argForSuperclass):
         Parent.__new__(argForSuperclass)
         self.field = myArg

But while

p = Parent("argForSuperclass")

works as expected

c = Child("myArg", "argForSuperclass")

fails, because ‘Child’ tries to call the __new__ method it inherits from ‘Parent’ instead of its own __init__ method.

What do I have to change in ‘Child’ to get the expected behavior?

  • 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-04T15:12:15+00:00Added an answer on June 4, 2026 at 3:12 pm

    Firstly, it is not considered best practice to override __new__ exactly to avoid these problems… But it is not your fault, I know. For such cases, the best practice on overriding __new__ is to make it accept optional parameters…

    class Parent(object):
        def __new__(cls, value, *args, **kwargs):
            print 'my value is', value
            return object.__new__(cls, *args, **kwargs)
    

    …so children can receive their own:

    class Child(Parent):
        def __init__(self, for_parent, my_stuff):
            self.my_stuff = my_stuff
    

    Then, it would work:

    >>> c = Child(2, "Child name is Juju")
    my value is 2
    >>> c.my_stuff
    'Child name is Juju'
    

    However, the author of your parent class was not that sensible and gave you this problem:

    class Parent(object):
        def __new__(cls, value):
            print 'my value is', value
            return object.__new__(cls)
    

    In this case, just override __new__ in the child, making it accept optional parameters, and call the parent’s __new__ there:

    class Child(Parent):
        def __new__(cls, value, *args, **kwargs):
            return Parent.__new__(cls, value)
        def __init__(self, for_parent, my_stuff):
            self.my_stuff = my_stuff
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class library project that has any number of supplemental
Let's say I have an abstract parent class called shape, and that there are
let's say I have a class that does some calculations. This set of calculations
Let's assume that a user votes for some movies in a scale of 1
Let's say I have one class User, and it has a property of type
Let's say we have a table that has 2 million rows. It has two
Let's say I want to have some kind of a cache that did something
Let's assume we have the following class : public class ImageButton extends MovieClip {
let us assume that I have a reusable business layer that further makes use
Let's say we have 3 classes: A, B and C. Each class has the

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.