Skip to content

Instantly share code, notes, and snippets.

@cengizhancaliskan
Created November 18, 2019 13:42
Show Gist options
  • Save cengizhancaliskan/1d3ffc4fff02444934513d7a48b08c97 to your computer and use it in GitHub Desktop.
Save cengizhancaliskan/1d3ffc4fff02444934513d7a48b08c97 to your computer and use it in GitHub Desktop.
Python call functions dynamically with getattr
class Cleaner(object):
def clean_name(self):
pass
cleaner = Cleaner()
for method_name in methods:
getattr(cleaner, method_name)()
### More Objective way
class Cleaner(object):
def __init__(self, fields):
self.fields = fields
def call_method(self):
for f in self.fields:
getattr(self, f)()
cleaner = Cleaner(['one', 'two'])
cleaner.call_method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment