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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:16:46+00:00 2026-05-11T04:16:46+00:00

I have the need to take a string argument and create an object of

  • 0

I have the need to take a string argument and create an object of the class named in that string in Python. In Java, I would use Class.forName().newInstance(). Is there an equivalent in Python?


Thanks for the responses. To answer those who want to know what I’m doing: I want to use a command line argument as the class name, and instantiate it. I’m actually programming in Jython and instantiating Java classes, hence the Java-ness of the question. getattr() works great. Thanks much.

  • 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. 2026-05-11T04:16:47+00:00Added an answer on May 11, 2026 at 4:16 am

    Reflection in python is a lot easier and far more flexible than it is in Java.

    I recommend reading this tutorial (on archive.org)

    There’s no direct function (that I know of) which takes a fully qualified class name and returns the class, however you have all the pieces needed to build that, and you can connect them together.

    One bit of advice though: don’t try to program in Java style when you’re in python.

    If you can explain what is it that you’re trying to do, maybe we can help you find a more pythonic way of doing it.

    Here’s a function that does what you want:

    def get_class( kls ):     parts = kls.split('.')     module = ".".join(parts[:-1])     m = __import__( module )     for comp in parts[1:]:         m = getattr(m, comp)                 return m 

    You can use the return value of this function as if it were the class itself.

    Here’s a usage example:

    >>> D = get_class("datetime.datetime") >>> D <type 'datetime.datetime'> >>> D.now() datetime.datetime(2009, 1, 17, 2, 15, 58, 883000) >>> a = D( 2010, 4, 22 ) >>> a datetime.datetime(2010, 4, 22, 0, 0) >>>  

    How does that work?

    We’re using __import__ to import the module that holds the class, which required that we first extract the module name from the fully qualified name. Then we import the module:

    m = __import__( module ) 

    In this case, m will only refer to the top level module,

    For example, if your class lives in foo.baz module, then m will be the module foo
    We can easily obtain a reference to foo.baz using getattr( m, 'baz' )

    To get from the top level module to the class, have to recursively use gettatr on the parts of the class name

    Say for example, if you class name is foo.baz.bar.Model then we do this:

    m = __import__( "foo.baz.bar" ) #m is package foo m = getattr( m, "baz" ) #m is package baz m = getattr( m, "bar" ) #m is module bar m = getattr( m, "Model" ) #m is class Model 

    This is what’s happening in this loop:

    for comp in parts[1:]:     m = getattr(m, comp)     

    At the end of the loop, m will be a reference to the class. This means that m is actually the class itslef, you can do for instance:

    a = m() #instantiate a new instance of the class     b = m( arg1, arg2 ) # pass arguments to the constructor     
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You need the 'RequestContext' to find all the route values.… May 11, 2026 at 11:56 am
  • added an answer Final solution was found by slightly restructuring the original query… May 11, 2026 at 11:56 am
  • added an answer From the MSDN documentation on Color.operator ==: This method compares… May 11, 2026 at 11:56 am

Related Questions

I have the need to take a string argument and create an object of
I have a text and I need to take the content in a defined
I have the need to read the Thunderbird address book on the fly. It
I have the need to copy the entire contents of a directory on a
I have the need to create the functionality to allow my registered users to
I have the need to express simple conditionals as program input. The input must
I've got a VB.NET WinForms app in which I have the need to refer
I have the following code that I need to add an additonal object to
I have the following Query and i need the query to fetch data from
I have the following VB.net interface that I need to port to C#. C#

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.