Classes, instances, and Python 3.15
3.15.2
Use classes to keep state and behavior together, and see selected changes in the current Python series.
On this page
Instance attributes and methods
__init__ initializes an instance. self names the instance passed to a method. Use instance attributes for values that differ per object. A class attribute belongs to the class and is shared by instances until shadowed.
class OrderLine:
def __init__(self, quantity, unit_price):
self.quantity = quantity
self.unit_price = unit_price
def total(self):
return self.quantity * self.unit_price
print(OrderLine(3, 2.5).total())
7.5
Inheritance and composition
A subclass can override inherited behavior. super() delegates according to Python’s method resolution order. Use composition when an object should hold another object that provides a service, such as an order holding a list of order lines.
Selected changes in Python 3.15
Python 3.15 enables UTF-8 Mode by default and adds language features such as explicit lazy imports and unpacking in comprehensions. Explicitly specifying external data encodings remains useful. Ordinary import and standard comprehensions work in these examples.