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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:43:32+00:00 2026-05-16T20:43:32+00:00

My multiple-inheritance-fu is not strong. I am trying to create a superclass whose __init__

  • 0

My multiple-inheritance-fu is not strong. I am trying to create a superclass whose __init__ takes an optional named parameter and subclasses of it which also inherit from built-in types. Sadly, I appear to have no idea how to make this work:

>>> class Super(object):
    name = None
    def __init__(self, *args, name=None, **kwargs):
        self.name = name
        super().__init__(self, *args, **kwargs)

>>> class Sub(Super, int):
    pass

>>> Sub(5)
5

>>> Sub(5, name="Foo")
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    Sub(5, name="Foo")
TypeError: 'name' is an invalid keyword argument for this function

(I’ve also tried it without the super() call, but the result was the same.)

Perhaps someone with better knowledge of multiple inheritance can point me in the right direction?

Update:

Here is the solution I ended up with, based on Alex’s answer.

It’s still a little hacky (by changining __new__‘s signature partway through construction), but it will work as long as the superclass appears in the subclasses’ MROs before the built-in type and defining __new__ this way allows me to make subclasses which work with different built-in types without adding a separate __new__ to each one.

>>> class Super(object):
    name = None
    def __new__(cls, *args, name=None, **kwargs):
        inner = super().__new__(cls, *args, **kwargs)
        inner.name = name
        return inner

>>> class Sub(Super, int):
    pass

>>> Sub(5)
5

>>> Sub(5, name="Foo")
5

>>> _.name
'Foo'
  • 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-16T20:43:33+00:00Added an answer on May 16, 2026 at 8:43 pm

    You just cannot pass arbitrary parameters (whether positional or named ones) to an equally arbitrary superclass (i.e., “immediately previous type in the mro of whatever the current leaf type is”) — most types and classes just don’t accept arbitrary parameters, for excellent reasons too — quoting from the middle of the Zen of Python,

    Errors should never pass silently.
    Unless explicitly silenced.
    

    and in most cases, calling (e.g.) int(name='booga') would of course be an error.

    If you want you weirdly-named class Super to be able to “pass on” arbitrary parameters, you must also ensure that all classes ever used as bases after it can handle that — so, for example, int can be called with one parameter (or exactly two: a string and a base), so, if it’s absolutely crucial to you that class Sub can multiply inherit from the buck-passing Super and int, you have to field that, e.g.:

    class Int(int):
        def __new__(cls, *a, **k):
            return int.__new__(Int, a[0] if a else 0)
    

    Note that you must override __new__, not __init__ (it does no harm if you also override the latter, but it’s irrelevant anyway): int is immutable so the value has to be set at __new__ time.

    Now, things like

    >>> class X(Super, Int): pass
    ... 
    >>> X(23, za='zo')
    23
    >>> 
    

    work. But note that X must subclass from Int (our __new__-sanitizing version of int), not from int itself, which, quite properly, has an unforgiving __new__!-)

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

Sidebar

Related Questions

I know the memory layout of multiple inheritance is not defined, so I should
Since multiple inheritance is bad (it makes the source more complicated) C# does not
Java doesn't support multiple inheritance for abstract classes, but I'm trying to do the
Why is multiple inheritance not supported in most of programming language? I could really
I've seen some discussion on why c# does not implement multiple inheritance but very
I understand that multiple inheritance 1 is simply not supported in PHP, and while
I know that C# does not offer multiple inheritance. And I know there' are
To be clear, I'm not asking if/why multiple inheritance is good or bad. I've
Since java does not support multiple inheritance I have a problem getting this to
I know that multiple inheritance is not allowed in Java and C#. Many books

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.