Skip to content
BENIBIX Learn

Method (Programming)

Development🧭 Programming ParadigmsObject-Oriented Programming

Also known as Method, Member Function

A function that belongs to an object. It can use that object's data.

Think of it like

Think of it like a trick a pet knows. You say the word, and that pet does its trick.

Example

Lucy calls upper() on the word hello. The string method gives back HELLO, and the original word is unchanged.

Why it matters

Methods keep each action next to the data it works on. That makes code easier to find and change.

Definition

A method is a function that belongs to an object or a class. It can read and change that object's own data. You call it with a dot after the object, like rex.speak() or "hello".upper(). The same method name can do different things on different kinds of object. In Python, a method's first input is usually called self, which means the object itself.

Example code

python
>>> "hello".upper()
'HELLO'
upper is a method that every string has.

Common mistake

Forgetting the brackets. rex.speak names the method; rex.speak() runs it.

Key takeaways

  1. 1
    Belongs to an object. A function with a home.
  2. 2
    Uses its data. Through self or this.
  3. 3
    Called with a dot. Like rex.speak().

Compare