Skip to main content

How to Get an Attribute of an Object in Python

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Many Python objects carry values of their own, called attributes. A date knows its year, its month and its day, and you read each one with a dot and its name: launch.year. When the name is only known while the program runs, getattr() reads it from a string instead. This guide does both on Python 3.15.2 and shows the errors for an attribute that doesn't exist.

Syntax​

Syntax
<object>.<attribute>
getattr(<object>, <name>, <default>)
PartWhat it means
<object>The object whose attribute you want, or a variable that holds it
.<attribute>The attribute's name, written straight into the code after a dot
getattr()A built-in function that reads an attribute by a name given as text
<name>The attribute's name as a string, such as "month", or a variable holding that string
<default>Optional. What getattr() returns when the object has no attribute by that name

Python's glossary describes an attribute as "A value associated with an object which is usually referenced by name using dotted expressions." The documentation of getattr() says the two forms match: getattr(x, 'foobar') "is equivalent to x.foobar".

The Example​

A variable called launch holds the date 5 December 2026. The first script prints its year, month and day. The second reads the month with getattr(), taking the attribute's name from a variable, and asks for an hour that a date doesn't have, with a default to fall back on.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Read Attributes with a Dot​

attributes.py
from datetime import date

launch = date(2026, 12, 5)

print(launch.year)
print(launch.month)
print(launch.day)

The attributes.py script in a macOS-style code window: it imports date from datetime, stores date(2026, 12, 5) in launch, then prints launch.year, launch.month and launch.day.

from datetime import date brings in the date type from Python's standard library, and date(2026, 12, 5) makes a date object from a year, a month and a day. Each print() line reads one attribute of that object.

Terminal
uv run python attributes.py
Output
2026
12
5

The terminal after uv run python attributes.py: 2026, then 12, then 5.

The three attributes came back as plain numbers. The datetime documentation lists them under a date's "Instance attributes (read-only)": year, month and day. The command works unchanged in PowerShell.

Step 2: Read an Attribute by Its Name with getattr()​

getattr_demo.py
from datetime import date

launch = date(2026, 12, 5)
part = "month"

print(getattr(launch, part))
print(getattr(launch, "hour", "no hour"))

The getattr_demo.py script in a macOS-style code window: launch is date(2026, 12, 5), part is the text month, then the script prints getattr(launch, part) and getattr(launch, "hour", "no hour").

part holds the text "month", so getattr(launch, part) reads the same attribute as launch.month. Your program could just as well have read that name from a file or from what a person typed. The second call asks for hour, which a date doesn't have, and gives "no hour" as the default.

Terminal
uv run python getattr_demo.py
Output
12
no hour

The terminal after uv run python getattr_demo.py: 12, then no hour.

12 is the month. For hour there was no attribute, so getattr() returned the default. The documentation explains: "If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised."

Common Mistakes​

MistakeWhat happensDo this instead
Reading an attribute the object doesn't have: launch.hourAttributeError: 'datetime.date' object has no attribute 'hour'Check the type's documentation for its attributes. A date has no time of day
Leaving out the default: getattr(launch, "hour")The same AttributeError: 'datetime.date' object has no attribute 'hour'Pass a default as the third argument when the attribute may be missing
Giving a read-only attribute a new value: launch.year = 2027AttributeError: attribute 'year' of 'datetime.date' objects is not writableMake a new date instead: launch.replace(year=2027) returned 2027-12-05
Leaving the parentheses off a method: print(launch.weekday)Python prints the method instead of running it: <built-in method weekday of datetime.date object at 0x104d67fb0>, with a different address after at on each runCall it: launch.weekday()

FAQ​

What's the difference between an attribute and a method?​

An attribute holds a value, so you read it with a dot and nothing more: launch.year gave 2026. A method is a function that belongs to the object, and the parentheses run it: launch.weekday() printed 5. The documentation says weekday() returns "the day of the week as an integer, where Monday is 0 and Sunday is 6", so 5 December 2026 is a Saturday.

Can I change an attribute?​

That depends on the object. A date's attributes are read-only, as the mistake above shows. Its replace() method returns "a new date object with the same values, but with specified parameters updated": launch.replace(year=2027) printed 2027-12-05, and launch itself still printed 2026-12-05.

Conclusion​

launch.year, launch.month and launch.day read 2026, 12 and 5 from a date. getattr(launch, part) read the month by a name kept in a variable, and getattr(launch, "hour", "no hour") returned its default because dates have no hour. Without the default, a missing attribute raises AttributeError.

Additional Resources​