[go: up one dir, main page]

Attributes are everywhere in Python PREMIUM

Series: Classes
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.10—3.14
Python Morsels
Watch as video
02:30

We use attributes everywhere in Python.

Accessing attributes

We have a class called Product:

class Product:

    unknown_price = "Ask for details"

    def __init__(self, name, price=None):
        self.name = name
        self.price = price

    def display_price(self):
        if self.price is None:
            return self.unknown_price
        return f"{self.price:.2f}"

We can make an instance of this class by calling it:

>>> duck = Product(name="rubber duck", price=5)
>>> duck
<product.Product object at 0x7fa808041eb0>

This Product instance has a name attribute and it has a price attribute:

>>> duck.name
'rubber duck'
>>> duck.price
5

An attribute looks like something.something, in this case duck.name and duck.price.

Attributes on classes

That duck variable points to a Product object (an instance of the Product class). Class instances (like duck) have attributes, but many other Python objects have attributes as well. For example, our Product class actually has attributes.

The Product class has an unknown_price attribute:

>>> Product.unknown_price
'Ask for details'

So we can look up attributes on class instances, but we can also lookup attributes on classes.

Module attributes

So class instances can have attributes and classes can have attributes, but attributes show up in other places too.

For example, modules have attributes:

>>> import math
>>> math.pi
3.141592653589793

Accessing math.pi looks up the pi attribute in the math module.

And math.e looks up the e attribute in the math module:

>>> math.e
2.718281828459045

Whenever you see x.y, that's an attribute lookup, no matter what that x is.

Function attributes

Even functions have attributes in Python.

We have a greet function:

>>> def greet(name="world"):
...     """Greet a user, or the whole world."""
...     print("Hello", name)
...

If we call it with no arguments, we'll see Hello world:

>>> greet()
Hello world

If we call it with an argument (representing a name), it's going to greet someone by their name:

>>> greet("Trey")
Hello Trey

The greet function has a __defaults__ attribute:

>>> greet.__defaults__
('world',)

That __defaults__ attribute is a tuple, that represents the default values of this function's arguments.

And in fact, greet also has a __doc__ attribute, which is the documentation for this function:

>>> greet.__doc__
'Greet a user, or the whole world.'

That __doc__ attribute stores the docstring that we wrote when defining the function.

Summary

So anytime you see x.y in Python, you're looking up the y attribute on x. x maybe a class instance, a class, a module, or a function. Whatever x is, it's something that has an attribute called y.

So attributes are all over the place in Python. Modules, functions, classes, and class instances all have attributes.

Anytime you see something.something_else, that's an attribute lookup.

Python Morsels
Watch as video
02:30
This is a free preview of a premium screencast. You have 2 previews remaining.