How to Get an Attribute of an Object in Python
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
<object>.<attribute>
getattr(<object>, <name>, <default>)
| Part | What 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
from datetime import date
launch = date(2026, 12, 5)
print(launch.year)
print(launch.month)
print(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.
uv run python attributes.py
2026
12
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()
from datetime import date
launch = date(2026, 12, 5)
part = "month"
print(getattr(launch, part))
print(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.
uv run python getattr_demo.py
12
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
| Mistake | What happens | Do this instead |
|---|---|---|
Reading an attribute the object doesn't have: launch.hour | AttributeError: '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 = 2027 | AttributeError: attribute 'year' of 'datetime.date' objects is not writable | Make 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 run | Call 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
- attribute — the glossary's definition.
- getattr() — the built-in function and its default.
- Attribute references — the reference for the dot.
- date objects — the attributes and methods of a date.
