Python

Python Menu

Python

Python hasattr() Function - Definition, Syntax, Parameters, Examples

Definition

The hasattr() function returns True if the specified object has the specified attribute, otherwise False.

Syntax

hasattr(object, attribute)

Parameters

Parameter Description
object Required. An object.
attribute The name of the attribute to check if exists

Examples

class Car: name = "Nissan" model = 1987 country = "Japan" result = hasattr(Car, 'model') falseResult = hasattr(Car, 'age') print(result) print(falseResult)

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods