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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:36:07+00:00 2026-06-13T12:36:07+00:00

I am trying to define in python a class B, subclass of A, that

  • 0

I am trying to define in python a class B, subclass of A, that could accept multiple ways of being instantiated to be very flexible. A is a simple class with few members. I know how to write A to allow a mix of positional/keywords arguments and having default values if some arguments are omitted.

My problem comes with class B. B is simply A with an extra member. But when I create an instance of B, I would like to have the choice between:

  • an instance created from a mix of positional/keyword arguments (pretty much like A)
  • an instance created specifying the extra member and an existing instance (or a copy) of A
  • created using only an existing instance of A and the extra member gets a default value.
  • making mutually exclusive the use of an existing instance of A and positional/keywords arguments

edit: something like this

class A:
    def __init__(self, x1=1, x2='a', x3=4.0)
        self.x1=x1
        self.x2=x2
        self.x3=x3

class B(A):
    def __init__(self, x1=1, x2='a', x3=4.0, x4='t')
        self.x4=x4
        A.__init__(x1,x2,x3)

but then I would like to use it like this:

a1=A()
b1=B()         # takes all defaults
b2=B(x4='r')   # x1,x2,x3 gets defaults from A
b3=B(a1,x4='z')  # use a1, an instance of A
b4=B(x1,x2,x3,x4) # defines manually all arguments
b5=B(a1,x1=2) # this one should not work because x1 can come from the instance a1 or x1
  • 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-13T12:36:08+00:00Added an answer on June 13, 2026 at 12:36 pm

    The simplest thing is probably to define B with a nice straightforward initialiser with the same signature as A uses and then additional factory functions defined as class methods to do the more complicated scenarios. That way you won’t get messed up when two different methods of construction actually take exactly indistinguishable arguments.

    So something like:

    class B(A):
        def __init__(self, some, args):
            A.__init__(self, some, args)
    
        @classmethod
        def fromA(cls, theA, extra=some_default):
            newB = cls(theA.some, theA.args)
            newB.extra = extra
            return newB
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define my own (very simple) exception class in Python 2.6, but
I'm trying to define a method in my vb.net interface that will only accept
I am trying to define an abstract class that has operators to compare two
I'm trying to define a very simple function in Drools as below: import java.util.List;
I'm trying to write a Python class that has the ability to do the
I'm trying to define a simply Fraction class And I'm getting this error: python
I'm trying to define some methods in a base class that can then be
I am new to Python. I was trying to define and run a simple
I'm trying to define a service endpoint in my web.config file so that I
I am trying to determine the owning class of a calling function in python.

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.