liveBook · Manning

Method

As explained in Python How-To

A method is a function defined as an attribute of a class and intended to operate on instances or the class itself.

Need a task to mark itself complete or accept a new tag? Define that behavior in its class. A method is a function defined on a class to provide behavior related to its data. Choosing the right kind of method makes the class easier to use and maintain. section 8.0 section 8.2

Choose a method according to what its work depends on:

  • Use an instance method when an operation reads or changes one instance’s data. Its first parameter is conventionally named self; call it as instance.method(arguments). section 8.2.1
  • Use a static method when the operation does not rely on an instance’s data. Mark it with @staticmethod. section 8.2.1 section 8.6
  • A class method is a third kind, marked with @classmethod. Creating an instance from a tuple is an exercise in choosing a suitable method kind. section 8.6 section 8.2.5
Listing 8.3 Setting all attributes in __init__
class Task:
   def __init__(self, title, desc, urgency):
       self.title = title
       self.desc = desc
       self.urgency = urgency
       self.status = "created"
       self.tags = []

   def complete(self):
       self.status = "completed"

   def add_tag(self, tag):
       self.tags.append(tag)

The Task class initializes status and tags in __init__, then changes those existing attributes through complete and add_tag. Initializing them first makes every new instance’s available attributes clear. section 8.1.3

Call a method on one task

With the Task class shown above: ```python task = Task("Laundry", "Wash clothes", 3) task.complete() print(task.status) # output: completed task.add_tag("home") print(task.tags) # output: ['home'] ``` Both calls act on task. Inside either method, self refers to that instance; an instance-method call is processed as a class call that passes the instance as an argument. section 8.1.3 section 8.2.1

Methods can also shape how instances appear. Define __str__ to return a descriptive string for print or an f-string; define __repr__ to return an instantiation-oriented string for the interactive console. Both are instance methods because their results depend on the particular object. section 8.4.1 section 8.4.2

Keep methods with the data and responsibilities they serve. If a class accumulates unrelated groups of methods and attributes, diagramming those groups can help reveal its separate functions. When testing a custom class, focus mainly on its methods. section 9.5.1 section 13.4

How Python How-To covers it

How deep it goes
Explains instance-method calls through self and method selection, then illustrates attribute initialization and special-method behavior.
Examples
Uses task completion and tagging, string representations, an enumeration’s display, and a tuple-to-instance challenge.
What it assumes
Assumes familiarity with Python functions, classes, and instance attributes while explaining self and how instance-method calls work.
What you can do after
Build it from scratch: choose and define instance methods that operate on existing instance attributes, and recognize when a static method fits better.
How it connects
Links methods to instance attributes, static methods, class methods, special methods, UML diagrams, and class testing.

Also explained in

Compare how 20 books explain method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.