Search code examples
pythongetattr

What does this (simple?) expression in Python mean? func(self)(*args)


I saw some Python code like: getattr(self, that)(*args). what does it mean? I see that the builtin getattr function gets called, passing the current object and that; but what is the (*args) doing after that? Does it call something with *args as parameter?


Solution

  • You're right on track. that is going to be the name of a method on the object. getattr() is returning that method (function) and then calling it. Because functions are first class members they can be passed around, returned, etc.