Table of contents
1.
Introduction
2.
Getattr Function
3.
Python Getattr Function
4.
Frequently Asked Questions
4.1.
What is Python?
4.2.
Is Python beginner-friendly?
4.3.
What is the Python getattr function?
4.4.
What are the parameters present in the Python getattr function?
4.5.
What is the role of the default parameter in the Python getattr function?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Getattr() Function in Python

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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!

Getattr function in Python

Let us discuss the Python getattr function in detail.

Must read, Divmod in Python, and Convert String to List Python

Getattr Function

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
Run Code


In the above snippet, Ninja is the object name, and it has attributes, namely,

  • Company
  • Domain
  • Headquarters
     

We can use the Python getattr function to fetch the value for any of these attributes.

Let us now look at an example of how to use the getattr function.

Also see, Merge Sort Python

Python Getattr Function

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,

getattr(Object Name, "Attribute Name", "Default Message")
You can also try this code with Online Python Compiler
Run Code


Note: Notice that the attribute name is provided in double-quotes.

Workflow of getattr function

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
Run Code


Output

Output

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
Run Code


Output

Error Output

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
Run Code


Output

Output for the default parameter

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. 

You can compile it with online python compiler.

You can also know about Python Square Root here.

Frequently Asked Questions

What is Python?

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. 

Recommended Reading:

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. 

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!

Cheers!

Live masterclass