Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is getattr() exactly and how do I use it?

Tags:

python

getattr

I've recently read about the getattr() function. The problem is that I still can't grasp the idea of its usage. The only thing I understand about getattr() is that getattr(li, "pop") is the same as calling li.pop.

I didn't understand when the book mentioned how you use it to get a reference to a function without knowing its name until run-time. Maybe this is me being a noob in programming, in general. Could anyone shed some light on the subject? When and how do I use this exactly?

like image 770
Terence Ponce Avatar asked Nov 02 '10 05:11

Terence Ponce


People also ask

What is a Getattr () used for?

Definition. Python getattr() is a built-in function that is used to return the value of an attribute of a specific object/instance. The Python getattr() function is used to obtain an object's attribute value and also provides the option of executing the default value if the attribute is not available.

Why Getattr is used in Python?

A significant reason to use getattr() in Python is you can manually get the attribute name as input from your console and use the attribute name as a string in getattr directly to get the value. And in case of an absent attribute, you can specify a default value, helping us fill in some of the blanks in our data.

What does __ Getattr __ do in Python?

__getattr__Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self ). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception.

What is Setattr () and Getattr () used for?

Python setattr() and getattr() goes hand-in-hand. As we have already seen what getattr() does; The setattr() function is used to assign a new value to an object/instance attribute.


1 Answers

Objects in Python can have attributes -- data attributes and functions to work with those (methods). Actually, every object has built-in attributes (try dir(None), dir(True), dir(...), dir(dir) in Python console).

For example you have an object person, that has several attributes: name, gender, etc.

You access these attributes (be it methods or data objects) usually writing: person.name, person.gender, person.the_method(), etc.

But what if you don't know the attribute's name at the time you write the program? For example you have attribute's name stored in a variable called attr_name.

if

attr_name = 'gender' 

then, instead of writing

gender = person.gender 

you can write

gender = getattr(person, attr_name) 

Some practice:

Python 3.4.0 (default, Apr 11 2014, 13:05:11)  >>> class Person(): ...     name = 'Victor' ...     def say(self, what): ...         print(self.name, what) ...  >>> getattr(Person, 'name') 'Victor' >>> attr_name = 'name' >>> person = Person() >>> getattr(person, attr_name) 'Victor' >>> getattr(person, 'say')('Hello') Victor Hello 

getattr will raise AttributeError if attribute with the given name does not exist in the object:

>>> getattr(person, 'age') Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: 'Person' object has no attribute 'age' 

But you can pass a default value as the third argument, which will be returned if such attribute does not exist:

>>> getattr(person, 'age', 0) 0 

You can use getattr along with dir to iterate over all attribute names and get their values:

>>> dir(1000) ['__abs__', '__add__', ..., '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']  >>> obj = 1000 >>> for attr_name in dir(obj): ...     attr_value = getattr(obj, attr_name) ...     print(attr_name, attr_value, callable(attr_value)) ...  __abs__ <method-wrapper '__abs__' of int object at 0x7f4e927c2f90> True ... bit_length <built-in method bit_length of int object at 0x7f4e927c2f90> True ...  >>> getattr(1000, 'bit_length')() 10 

A practical use for this would be to find all methods whose names start with test and call them.

Similar to getattr there is setattr which allows you to set an attribute of an object having its name:

>>> setattr(person, 'name', 'Andrew') >>> person.name  # accessing instance attribute 'Andrew' >>> Person.name  # accessing class attribute 'Victor' >>> 
like image 129
warvariuc Avatar answered Sep 20 '22 02:09

warvariuc