PythonBuilt-ingetattr
hasattr

getattrBuilt-in

vars

Returns the value of the named attribute of an object.

Syntax

getattr(object, name, default?)

Example

Enter values below to update the example in real time.

class
Dog
def
__init__
self
name
dog
print
getattr
Rex
unknown
class Dog:
    def __init__(self):
        self.name = "Rex"

dog = Dog()
print(getattr(dog, "name"))            # Rex
print(getattr(dog, "age", "unknown"))  # unknown