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



















