iTayb
iTayb

Reputation: 12743

How to get function object by name?

I do want to process a command with an argument specified by the user. I thought about:

self.urlRegexFunc = "endswith"
self.urlRegex = ".mp3"
exec('b = attr[1].%s("%s")' % (self.urlRegexFunc, self.urlRegex)) # attr[1] is string
if b:
    pass # Do Something

But I get:

SyntaxError: unqualified exec is not allowed in function 'start_a' it contains a nested function with free variables

What can I do?

Upvotes: 2

Views: 130

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798556

You're looking for getattr().

Upvotes: 2

Related Questions