Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
We know how engaging the language of Python is! It is a versatile language that does wonders. Do you know about the Python getattr function? If not, don't worry. We got you covered!
Let us discuss the Python getattr function in detail.
The getattr function is a very potent tool in programming languages. It is used to fetch the values associated with a particular attribute of an object. The Python getattr function is preferred cause of the fact that it can generate default values for attributes that are not present in the object. Let us look at the structure of an object,
class Ninja:
company = "Coding Ninjas"
domain = "Education Tech"
headquarters = "New Delhi, India"
You can also try this code with Online Python Compiler
The getattr function is a default function present in Python that can be incorporated into the code without importing any additional libraries. The syntax for the Python getattr function looks like this,
Note: Notice that the attribute name is provided in double-quotes.
Let us look at an example of the Python getattr function.
class Ninja:
company = "Coding Ninjas"
domain = "Education Tech"
headquarters = "New Delhi, India"
print('The company is : ', getattr(Ninja, "company"))
print('The domain is : ', getattr(Ninja, "domain"))
You can also try this code with Online Python Compiler
In the above code, we used the Python getattr function. We fetched the value of attributes company and domain.
Now that we know about the Python getattr function let us assume a scenario. Let us assume we have an object named “Ninja”, but we are not sure if it contains the attribute named “revenue”. In case “Ninja” does not contain “revenue”, the code might give an error. For example,
class Ninja:
company = "Coding Ninjas"
domain = "Education Tech"
headquarters = "New Delhi, India"
print('The revenue is : ', getattr(Ninja, "revenue"))
You can also try this code with Online Python Compiler
In the above example, since “revenue” was not present as an attribute, the compiler returned an error. To tackle this situation, we can use another parameter of the Python getattr function, which is the “default” parameter. Let us look at an example,
class Ninja:
company = "Coding Ninjas"
domain = "Education Tech"
headquarters = "New Delhi, India"
print('The company is : ', getattr(Ninja, "company"))
print('The revenue is : ', getattr(Ninja, "revenue", "Attribute Not Present"))
You can also try this code with Online Python Compiler
As you can notice, we do not get any error thrown from the compiler side. The default value is returned in case the attribute is not present in the object.
Python is a general-purpose programming language. It is a high-level programming language. Python emphasises on code readability with the usage of indentation.
Is Python beginner-friendly?
Yes, Python is a beginner-friendly language. It is one of the most popular programming languages. Python is easy to understand as compared to other languages.
What is the Python getattr function?
The python getattr function is used to fetch the value for the specified attribute present in an object.
What are the parameters present in the Python getattr function?
The Python getattr function has three parameters, namely, object, attribute, and default parameter.
What is the role of the default parameter in the Python getattr function?
The default parameter is used to return a custom message when an attribute is not present in the object.
Conclusion
In this article, we discussed the Python getattr function. We also discussed the parameters associated with the Python getattr function. We further looked at various scenarios while working with the Python getattr function.
Have a look at the interview experiences and interview bundle for placement preparations. Nevertheless, consider our paid courses to give your career an edge over others!