Created
November 18, 2019 13:42
-
-
Save cengizhancaliskan/1d3ffc4fff02444934513d7a48b08c97 to your computer and use it in GitHub Desktop.
Python call functions dynamically with getattr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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