Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
We use attributes everywhere in Python.
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.
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.
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.
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.
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.
Need to fill-in gaps in your Python skills?
Sign up for my Python newsletter where I share one of my favorite Python tips every week.
Classes are a way to bundle functionality and state together.
The terms "type" and "class" are interchangeable: list, dict, tuple, int, str, set, and bool are all classes.
You'll certainly use quite a few classes in Python (remember types are classes) but you may not need to create your own often.
To track your progress on this Python Morsels topic trail, sign in or sign up.
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.