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

The Archive Base Latest Questions

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

I have a file different_classes that contains three different classes. It is something like:

  • 0

I have a file different_classes that contains three different classes. It is something like:

class first(object):
    def __init__(x, y, z):
    body of the first class

class second(first):
    def __init__(x, y, z, a=2, b=3):
    body of the second class

class third(object):
    def __init__(x, y, z):
    body of the third class

Now I have another file, say main.py where I want to be able to pass on the name of the class that needs to be called. For example, right now I do:

import different_classes
def create_blah():
    instance = different_classes.first()
    rest of the function body

when I want to use the first class in different_classes. If I want to use class second, I use different_classes.second().

Can I input the class name as an argument in the create_blah function. Something like:

def create_blah(class_type = "first", x=x1, y=y1, z=z1):
    instance = different_classes.class_type(x, y, z)

I know this may not be valid…but want to know if something similar can be done. Thanks!

  • 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-18T20:00:33+00:00Added an answer on May 18, 2026 at 8:00 pm

    Rather than passing the name of the class, why not just pass the class itself:

    def create_blah(class_type = different_classes.first, x=x1, y=y1, z=z1):
        instance = class_type(x, y, z)
    

    Remember that a class is just an object like anything else in Python: you can assign them to variables and pass them around as arguments.

    If you really do need to use the name, e.g. because you are reading it from a configuration file, then use getattr() to retrieve the actual class:

    instance = getattr(different_classes, class_type)(x, y, z)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a package of plug-in style modules. It looks like this: /Plugins /Plugins/__init__.py
I have many, (15-20) different XML files that I need to load to VB.Net.
I have a Ruby code with different classes in a few files. In one
I wrote some classes that I use with many different projects. For example, I
I have 2 classes. One will be a basic instructions screen and on that
I want to store a graph of different objects for a game, their classes
I am working on a grails project and I have put a .jar file
I have a View Controller that is swapping UIView objects in and out. There
I am using a spinner when selected starts an intent and the class that
In C++ I have an array of pointers to Player objects and want to

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.