Skip to main content

method

Definition

A method is a function bound to an object. Accessing obj.method produces a bound method that remembers obj as its first argument. Methods can be inspected for their underlying function and the bound instance.

class Greeter:
def hi(self, name):
return f"hi {name}"

g = Greeter()
bound = g.hi # method bound to g
bound("Ada") # 'hi Ada'

Introspection

  • __self__ — the bound instance (or None for unbound functions retrieved from a class)
  • __func__ — the original function object
>>> bound.__self__ is g
True
>>> bound.__func__ is Greeter.hi
True

Dunder methods

Dunder MethodOperationExample (normal syntax)Example (dunder call)
__call__Call the method"hi".upper()'HI'"hi".upper.__call__()
__func__Underlying functionobj.method.__func__(attribute access)
__self__Bound instanceobj.method.__self__(attribute access)