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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:26:50+00:00 2026-05-21T19:26:50+00:00

how do i create object-instances on runtime in python? say i have 2 classes:

  • 0

how do i create object-instances on runtime in python?

say i have 2 classes:

class MyClassA(object):
    def __init__(self, prop):
        self.prop = prop
        self.name = "CLASS A"

    def println(self):
        print self.name


class MyClassB(object):
    def __init__(self, prop):
        self.prop = prop
        self.name = "CLASS B"

    def println(self):
        print self.name

and a dict

{('a': MyClassA), ('b': MyClassB)}

how can i create dynamic an instance of one of my two classes, depending of i choose ‘a’ or ‘b’.

kind of this:

somefunc(str):
    if 'a': return new MyClassA
    if 'b': return new MyClassB

to get “CLASS B” on calling: somefunc('a').println

but in a more elegant and dynamic way (say i add more classes to the dict on runtime)

  • 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-21T19:26:51+00:00Added an answer on May 21, 2026 at 7:26 pm

    You might create a dispatcher, which is a dictionary with your keys mapping to classes.

    dispatch = {
        "a": MyClassA,
        "b": MyClassB,
    }
    
    instance = dispatch[which_one]() # Notice the second pair of parens here!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to dynamically create and name instances of my object at runtime as
I am confused as to when to create object instances or Static Helper classes.
I have inherited a class in vb.net and when I create the object, I
I need to create at runtime instances of a class that uses generics, like
Say I create an object thus: var myObject = {ircEvent: PRIVMSG, method: newURI, regex:
How does the code looks that would create an object of class: string myClass
I have the need to take a string argument and create an object of
I have 2 instances of a class that implements the IEnumerable interface. I would
I have the need to extend instances of various types at runtime. Most of
Having a object x which is an instance of some class how to 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.