liveBook · Manning

Method

As explained in The Quick Python Book, Fourth Edition

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

A method is a function defined in a class. It gives instances behavior that can use their own data. Define one with `def` inside the class, then call it through an instance with `instance.method()`. For an ordinary instance method, the first parameter is conventionally named `self`; Python supplies the calling instance automatically. section 15.3 section 3.6

Calculate an area

In `class Circle`, `area(self)` returns `self.radius * self.radius * 3.14159`. After setting `c.radius = 3`, `c.area()` returns `28.27431`. The call supplies `c` as `self`; you do not write `c.area(c)`. section 15.3

class Circle:
    def __init__(self):
        self.radius = 1
    def area(self):
        return self.radius * self.radius * 3.14159
 
c = Circle()
c.radius = 3
print(c.area())
 
28.27431

The complete class and call show how a method reads an instance variable and returns a result. section 15.3

You can call the same method through its class as `Circle.area(c)`, explicitly passing the instance. This is less convenient and usually less clear than `c.area()`. Apart from `self`, methods can accept ordinary parameters, including default and keyword arguments. Keep `self.radius`, an instance variable, distinct from a parameter named `radius`. section 15.3

Methods also appear in these common roles:

  • `__init__` initializes a new instance; `__str__` supplies a string used by `print`. section 15.15 section 3.6
  • An inherited method remains available to a subclass unless overridden or made private; a `Circle` can use `Shape.move`. section 3.6 section 15.15
  • Static methods need no instance and receive no `self`. Class methods receive a class reference, conventionally named `cls`. section 15.15

Built-in types have methods too: `x.upper()` calls a string method on `x`. Because strings are immutable, string methods return results rather than changing the original string. Use the returned value. section 6.4

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains definitions, implicit instance binding, class-based calls, parameters, and method varieties without descending into implementation internals.
Examples
Uses a Circle area calculation, an inherited Shape move call, and string method calls.
What it assumes
Assumes general programming familiarity while explaining Python's explicit `self` convention.
What you can do after
Build it from scratch by defining an instance method and calling it through an object.
How it connects
Connects methods to instance variables, inheritance, special methods, static methods, class methods, and immutable strings.

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 Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.