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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:45:40+00:00 2026-05-28T19:45:40+00:00

Normal way: import ham ham.eggs.my_func() ham.sausage.my_func() How I want it Dynamic way: str =

  • 0

Normal way:

import ham
ham.eggs.my_func()
ham.sausage.my_func()

How I want it “Dynamic” way:

str = 'eggs'
ham.str.my_func()
str = 'sausage'
ham.str.my_funct()

Same thing, but ideally how I would use it in a loop:

for x in ['eggs', 'sausage']
    ham.x.my_func()

Basically, I want a string tell which class’s my_func to invoke?

  • 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-28T19:45:41+00:00Added an answer on May 28, 2026 at 7:45 pm
    getattr(ham, x).my_func()
    

    This gets the method/thing (Python is duck typed) from ham which was assigned as an attribute whose name was the contents of x (if x is 'foo', then it’ll be ham.foo), then invokes my_func on that

    If ham has no method with the right name, you’ll get an exception.

    Incidentally, your example of ham.str.my_func() should be getattr(ham, str).my_func().

    If you want to really do this the right way, you could use the imp method to only get members of a certain type, or use additional introspection.

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

Sidebar

Related Questions

Normal way: class A: def __init__(self): self.a.b.c = 10 def another_method(self): self.a.b.c = self.a.b.c
Is the normal way to do this to just make 2 cols in my
In javascript, is there any different between these two: // call MyFunction normal way
Is it normal to have tests that are way bigger than the actual code
Is there a way to tune the Oracle client (InstantClient or the normal one)?
What is the most concise way of converting a java.util.List into a normal JavaFX
What is the best way within my PHP script to differentiate between a normal
I'm looking for a way to determine the difference between two dates. A normal
Is there any difference in doing @class MyViewController; rather than doing the normal import
I want to fork an Android project (a normal application) from the official repository

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.