liveBook · Manning

Class

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A class is a Python construct that defines the structure and behavior of objects and provides a namespace for their attributes and methods.

A class packages data with the methods that act on it, turning a design into objects with recognizable state and behavior. This makes application functionality cleaner to use and easier to organize. section 4.1 section 4.2

Define a class as a template, then instantiate it whenever you need a concrete object. The __init__() method receives initial data, while self identifies the particular instance whose attributes and state a method uses. section 4.1.1

Representing people

A Person class can store first, middle, and last names and expose full_name() as its behavior. Creating several Person instances gives each person distinct state, even though all instances share the same class design. section 4.1.1

class Person:
    def __init__(self, fname: str, mname: str = None, lname: str = None):
        self.fname = fname
        self.mname = mname
        self.lname = lname
 
    def full_name(self) -> str:
        full_name = self.fname
        if self.mname is not None:
            full_name = f"{full_name} {self.mname}"
        if self.lname is not None:
            full_name = f"{full_name} {self.lname}"
        return full_name

Inspect the progressive Person definition: __init__() assigns attributes, and full_name() combines available values into a result. section 4.1.1

Use classes to structure larger designs without forcing every relationship into inheritance:

  • Treat class attributes and methods as public by default; Python relies on responsible use rather than protected or private access controls. section 4.1.1
  • Use inheritance when a child genuinely extends a parent, and use composition when added functionality would otherwise create an awkward hierarchy. section 4.3
  • Refactor related functions that operate on the same data into methods of a class when doing so improves readability and responsibility boundaries. section 3.2.5

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains definitions, instantiation, self, public members, inheritance, composition, and refactoring, without covering advanced class mechanics.
Examples
Uses a Person class with optional names, full_name(), multiple instances, and a UML representation.
What it assumes
Assumes familiarity with Python functions and data structures while teaching object construction, instance state, and class relationships.
What you can do after
Build it from scratch by defining classes, instantiating objects, assigning state, and implementing methods that operate on that state.
How it connects
Connects classes with objects, methods, encapsulation, namespaces, inheritance, composition, and the DRY principle.

Also explained in

Compare how 21 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.