Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch getattr function in python with additional parameters?

Tags:

python

getattr

I want to call some unknown function with adding parameters using getattr function. Is it possible?

like image 645
megido Avatar asked Jun 12 '11 12:06

megido


People also ask

How many arguments Getattr () function receives in Python?

getattr() is one of the coolest built-in functions in Python. It takes three arguments: an object. name of the object's attribute but in the string format.

Does Getattr work with methods?

Putting it to work: generalizing method calls Okay, so it's cool that you can use getattr to get methods as well as properties, but how does that help us? Well, this can definitely be useful in keeping your code DRY if you have some common logic surrounding branching method calls.

What would happen if Getattr () tried to retrieve an attribute of an object that did not exist?

The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.


1 Answers

Yes, but you don't pass them to getattr(); you call the function as normal once you have a reference to it.

getattr(obj, 'func')('foo', 'bar', 42) 
like image 171
Ignacio Vazquez-Abrams Avatar answered Nov 03 '22 07:11

Ignacio Vazquez-Abrams