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

  • Home
  • SEARCH
  • 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 9195321
In Process

The Archive Base Latest Questions

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

Lets say I have two different classes with a different number of arguments: class

  • 0

Lets say I have two different classes with a different number of arguments:

class A(object):
    def __init__(self, arg1, arg2, arg3)
    ...

class B (object):
    def __init__(self, arg1, arg2)
    ....

Is there a way to dynamically instantiate these classes, given there’s a difference in the number of arguments?

I’ll give you the bigger picture here. I’ve created a XML parser that has successfully parsed all info from an XML document and cached them into a list, based on tag names (<room> will go into self.room, <item> will go into self.item, etc…)

I don’t know if this actually the way of doing things, but what I’m trying to do is take all the info from each individual XML element in their respected list, create Python objects from that info, then store ’em into a dict.

# From XMLParser class
def objectify(self, iterator):
    dict = {}
    # I used elementtree to parse XML documents, 2nd argument of getattr
    # ends up being a capitalized string of iterator's tag name.
    # Ex, <room> becomes 'Room'
    Object = getattr(sys.modules[__name__], iterator.tag.capitalize())
    for element in iterator:
        dict[element.attrib['id']] = Object(...)

So how do i enter the different amount of arguments when dynamically instantiating the different classes, listed above?

Am I anywhere near the right track, or not?

  • 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-17T21:36:15+00:00Added an answer on June 17, 2026 at 9:36 pm

    This should be easy:

    klass = A
    args = (a, b, c)
    obj = klass(*args)
    

    You can put all this into a factory function or class.

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

Sidebar

Related Questions

I have an Interface lets say ISendOut which I've inherited two different classes from
Lets say I have two classes: public class A { public virtual int Id
lets say I have two matrices, of a different size: a = zeros(1,100); b
I have two total different implementations of AsyncTask , lets say AT1 and AT2
Lets say there are two pages with two different html elements which have the
Lets say we have two objects o1 & o2 defined as System.Object, in my
Let's say I have the following two classes: package example.model; public class Model {
I have following scenario: Lets say we have two different webparts operating on the
Lets say that I have two different types of RelativeLayouts. That is to say
Let's say I have two classes: class A { public: A* Hello() { return

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.