• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Check if Object Has Attribute

Python Check if Object Has Attribute

February 12, 2022 Leave a Comment

Using Python, the easiest way to check if object has an attribute is to use the Python hasattr() function.

if hasattr(obj, "upper"):
    print("Object has attribute upper!")
else:
    print("Object doesn't have attribute upper!")

We can also use exception handling to see if an object has an attribute in Python.

try:
    obj.upper()
    print("Object has attribute upper!")
except TypeError:
    print("Object doesn't have attribute upper!")

When working with objects in Python, it is useful to be able to easily check if an attribute exists in an object.

We can check if an object has an attribute with the Python hasattr() function. The hasattr() function will return if the object has the attribute or not.

Below are some examples of using the hasattr() function to check if different objects have various attributes.

print(hasattr("string","upper"))
print(hasattr(10,"upper"))
print(hasattr([1,2,3],"__iter__"))
print(hasattr({ "key1":"value1" },"lower"))

#Output:
True
False
True
False

Checking if an Object has an Attribute with Exception Handling in Python

Another way you can check if an object has an attribute is with exception handling in Python.

When we try to access an attribute in an object, and the attribute doesn’t exist, we will get an AttributeError. If we don’t get an AttributeError, we will know the object has the attribute.

Therefore, using this logic, we can check if an object has a particular attribute.

Below is an example in Python of checking if different objects have various attributes using exception handling.

try:
    0.lower()
    print('Object has attribute "lower"!')
except TypeError:
    print('Object doesn't have attribute "lower"!')

try:
    "power".lower()
    print('Object has attribute "lower"!')
except TypeError:
    print('Object doesn't have attribute "lower"!')

Object doesn't have attribute "lower"!
Object has attribute "lower"!

Hopefully this article has been useful for you to learn how to check if an object has an attribute or not in Python.

Other Articles You'll Also Like:

  • 1.  Format Numbers as Currency with Python
  • 2.  Using Python to Convert Float to Int
  • 3.  Remove Empty Lists from List in Python
  • 4.  Ceiling Function Python – Get Ceiling of Number with math.ceil()
  • 5.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 6.  Convert String to Boolean Value in Python
  • 7.  Check if a Number is Divisible by 2 in Python
  • 8.  Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 9.  Changing Python Turtle Size with turtlesize() Function
  • 10.  Count Values by Key in Python Dictionary

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy