liveBook · Manning

Method

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

A method is a function associated with an object or class and invoked in the context of that object or class.

A method is a function associated with a class or object. It gives an object behavior alongside the data that describes its state, helping keep related functionality together and making object-oriented APIs easier to use. section 4.1 section 3.2

A Person method

The Person class stores first, middle, and last names. Its full_name() method combines those attributes into a displayable name. Calling p1.full_name() invokes the method for one particular Person instance. The self parameter connects the method to that instance, so separate Person objects retain separate state. section 4.1.1

Methods are useful when several operations work on the same data. Instead of leaving related functions scattered across an API, a class can group them around the state they modify. When a function becomes difficult to understand, refactoring part of its work into smaller methods can produce clearer, more maintainable code. section 4.1 section 3.2.5

Practical method design:

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

How deep it goes
Defines methods, demonstrates self and instance state, and applies them through a concrete Person example.
Examples
Demonstrates Person.full_name(), separate Person instances, and a loop that prints names.
What it assumes
Assumes familiarity with functions while introducing classes, instantiation, self, and object state.
What you can do after
Apply it by defining class methods, instantiating objects, and calling behavior through instances.
How it connects
Connects methods with classes, objects, instances, attributes, encapsulation, and APIs.

Also explained in

Compare how 20 books explain method →

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.