getattr()Easy Examples

Returns the value of a named attribute of an object

Basic getattr() usage

Simple demonstration of the getattr() built-in function.

python
class Config:
    debug = True
    version = "1.0"

print(getattr(Config, "debug"))
print(getattr(Config, "missing", "default"))

getattr() is a built-in function that returns the value of a named attribute of an object.

getattr() with different inputs

Calling getattr() with various argument types.

python
# More getattr() examples
print("getattr() is a Python built-in function")
print(f"Type: {type(getattr)}")

getattr() accepts different types of arguments and produces corresponding results.

Want to try these examples interactively?

Open Easy Playground