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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:08:36+00:00 2026-06-08T14:08:36+00:00

I have the following class. func_list= [function1, function2, function3] class doit(object): def __init__(self): for

  • 0

I have the following class.

func_list= ["function1", "function2", "function3"]

class doit(object):
    def __init__(self):
        for item in func_list:
            if item == "function1":
                self.function1()
            elif item == "function2":
                self.function2()
            elif item == "function3":
                self.function3()

    def function1(self):
        #do this
        pass
    def function2(self):
        #do that
        pass
    def function3(self):
        pass

If an instance of this class is created, it iterates over a list of strings and calls methods depending on the actual string. The strings in the list have the names of the corresponding methods.

How can I do this in a more elegant way?
I don’t want to add another elif-path for every “function” I add to the list.

  • 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-06-08T14:08:38+00:00Added an answer on June 8, 2026 at 2:08 pm
    func_list= ["function1", "function2", "function3"]
    
    class doit(object):
        def __init__(self):
            for item in func_list:
                getattr(self, item)()
        def function1(self):
            print "f1"
        def function2(self):
            print "f2"
        def function3(self):
            print "f3"
    
    
    
    >>> doit()
    f1
    f2
    f3
    

    For also private functions:

    for item in func_list:
         if item.startswith('__'):
             getattr(self, '_' + self.__class__.__name__+ item)()
         else:
             getattr(self, item)()
    

    .

    getattr(object, name[, default])
    

    Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, ‘foobar’) is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

    http://docs.python.org/library/functions.html#getattr

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

Sidebar

Related Questions

I have the following class; class myStringMethod(): def __init__(self): self.func_list= [('func1','print_func1()'),('func2','print_func2()')] def print_func1(self, name):
Say I have the following: class Parent(object): [...] def func(self, list): for item in
I have the following situation in my python code: class Parent(object): def run(self): print
Given the following: class MyClass: def __init__(self, someval): self.someval = someval def get_a_function(self): def
I have the following method: class Store < ActiveRecord::Base def my_func(str) puts str end
I have the following object structure. public class Study { public Guid? PreviousStudyVersionId {
I have the following classes... class ExpressionBinder<T> { public Func<T> Getter; public Action<T> Setter;
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {
I have following UML class diagram and java coding for each, please tell me

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.