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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:46:13+00:00 2026-05-21T03:46:13+00:00

I came up with this solution. But this looks too complicated. There must be

  • 0

I came up with this solution. But this looks too complicated. There must be a better and easier way. And second : is there a way to dynamically import the class ?

class_name = "my_class_name"                # located in the module : my_class_name.py  
import my_class_name from my_class_name          

my_class = globals()[class_name]  
object = my_class()  
func = getattr(my_class,"my_method")
func(object, parms)                         # and finally calling the method with some parms
  • 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-21T03:46:14+00:00Added an answer on May 21, 2026 at 3:46 am

    Take a look at the __import__ built-in function. It does exactly what you expect it to do.

    Edit: as promised, here’s an example. Not a very good one, I just kinda got bad news and my head is elsewhere, so you’d probably write a smarter one with more practical application for your context. At least, it illustrates the point.

    >>> def getMethod(module, cls, method):
    ...     return getattr(getattr(__import__(module), cls), method)
    ... 
    >>> getMethod('sys', 'stdin', 'write')
    <built-in method write of file object at 0x7fcd518fa0c0>
    

    Edit 2: here’s a smarter one.

    >>> def getMethod(path):
    ...     names = path.split('.')
    ...     return reduce(getattr, names[1:], __import__(names[0]))
    ... 
    >>> getMethod('sys.stdin.write')
    <built-in method write of file object at 0x7fdc7e0ca0c0>
    

    Are you still around?

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

Sidebar

Related Questions

I came across this thread when I was looking for a solution, but it
This came up in Hidden features of Python , but I can't see good
I came across this today MPI_ERR_COUNT Invalid count argument. Count arguments must be non-negative;
Came across this error today. Wondering if anyone can tell me what it means:
This came up from this answer to a previous question of mine . Is
This came up when answering another user's question (TheSoftwareJedi)... Given the following table: ROW_PRIORITY
This came up in a conversation I was having online, and it occured to
This came up recently in a class for which I am a teaching assistant.
I came across this simple PHP Class on GitHub while searching for Bloom Filters,
I came across this article written by Andrei Alexandrescu and Petru Marginean many years

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.