{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. meta::\n", " :description: Topic: The different kinds of class methods, Difficulty: Medium, Category: Section\n", " :keywords: instance, class method, static method, property, abstract method, class funtion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Methods\n", "\n", "Recall that a method is an attribute of a class that is a function. For example, \"append\" is a method that is defined for the `list` class and \"capitalize\" is a method of the `str` (string) class. \n", "\n", "```python\n", "# create an instance of the `list` class/type\n", "# and invoke the instance method `append`\n", ">>> a = [1, 2, 3]\n", ">>> a.append(-1)\n", ">>> a\n", "[1, 2, 3, -1]\n", "\n", "# create an instance of the `str` class/type\n", "# and invoke the instance method `capitalize`\n", ">>> b = \"moo\"\n", ">>> b.capitalize()\n", "'Moo'\n", "```\n", "\n", "Here we will encounter three varieties of methods:\n", "\n", "- instance methods\n", "- class methods\n", "- static methods\n", "\n", "whose differences are relatively minor but are important to understand. The functions \"append\" and \"capitalize\" are both examples of instance methods, specifically, as they are designed to be invoked by a particular list instance and string instance, respectively.\n", "\n", "We have already worked with the instance method `__init__`, which is special in that it is reserved by Python to be executed whenever class-initialization is invoked. Similarly, the special instance method `__add__` informs how an object interacts with the `+` operator. For example, `float.__add__` specifies that `+` will sum the values of `float` instances, whereas `list.__add__` specifies that `+` will concatenate `list` instances together. We will conclude our discussion of methods by surveying a number of these special methods - they will greatly bolster our ability to define convenient, user-friendly classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Instance Methods\n", "An *instance method* is defined whenever a function definition is specified within the body of a class. This may seem trivial but there is still a significant nuance that must be cleared up, which is that '`self`' is the defacto first-argument for any instance method. This is something that we encountered when working with `__init__`. Let's proceed naively so that we will hit a very common error, which will bring this matter to light. We begin by creating a class with an instance method that simply accepts one argument and then returns that argument unchanged:\n", "\n", "```python\n", "class Dummy:\n", " def func(x): \n", " \"\"\" An instance method that returns `x` unchanged. \n", " This is a bad version of this instance method!\"\"\"\n", " return x\n", "``` \n", "\n", "We can call this method from the class object `Dummy` itself, and it will behave as-expected:\n", "```python\n", ">>> Dummy.func(2)\n", "2\n", "```\n", "but something strange happens when we try to call `func` from an instance of `Dummy`:\n", "```python\n", "# calling `func` from an instance of `Dummy` produces\n", "# an unexpected error\n", ">>> inst = Dummy()\n", ">>> inst.func(2)\n", "TypeError: func() takes 1 positional argument but 2 were given\n", "```\n", "At first glance, this error message doesn't seem to make any sense. It is indeed true that `func` only accepts one argument - we specified that it should accept the argument `x` in its function definition. How is it that `inst.func(2)` specifies *two* arguments? It seems like we are solely passing `2` to our method. Herein lies an extremely important detail:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "