Arya Stark
Arya Stark

Reputation: 243

How can i execute a list of methods on an object in Python

I have a list of methods which I have to execute on an Object. These are the methods

methods = ['plays', 'plays', 'is', 'is']
relation_dict = {}

I want to get a relation dictionary which looks like this

relation_dict = {'plays':FOAF.plays,'plays':FOAF.plays, 'is':FOAF.is, 'is':FOAF.is}

here FOAF is the object. How can we do this?

Upvotes: 0

Views: 56

Answers (2)

Don CorliJoni
Don CorliJoni

Reputation: 248

This is an oneliner way:

methods = ['plays', 'plays', 'is', 'is']
relation_dict = {method: getattr(FOAF, method) for method in methods}

Upvotes: 0

baskettaz
baskettaz

Reputation: 859

you could achive something similar like this :

obj = FOAF
for i in methods: 
   getattr(obj, i)() # <-- this will call the needed method

Good luck :)

Upvotes: 1

Related Questions