Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

Built-in getattr function

Thursday, May 30, 2013

 You can get a reference to a function without knowing its name until run-time, by using the getattr function.
 class Cls():  
   def getfood(self):  
     self.food = ['Cheese', 'Pizza', 'Burger', 'Hotdog', 'Pie', 'Fires']  
     return self.food  
 c = Cls()  
 print getattr(c, 'getfood')() 

The output of following code will be
 ['Cleese', 'Palin', 'Idle', 'Gilliam', 'Jones', 'Chapman']