Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. Function vs method

Python · Functions & Decorators

Function vs method

Mediumpython-14
functionmethodoopself

Question

What is the difference between a function and a method in Python?

Solution

A function is a callable defined with def or lambda at module (or local) scope. A method is a function defined inside a class body and accessed through an instance or class; it is bound to that object.

  • Function: len(x), sorted(items), helper(a, b).
  • Method: items.append(x), text.upper(), MyClass.from_config(...).

Bound instance methods receive the instance as self automatically. Calling obj.method creates a bound method; Class.method(obj, ...) is the unbound/manual form.

def greet(name):          # function
    return f"hi {name}"

class Greeter:
    def __init__(self, name):
        self.name = name

    def greet(self):      # instance method
        return f"hi {self.name}"

    @classmethod
    def default(cls):     # class method
        return cls("guest")

    @staticmethod
    def shout(msg):       # static method (no self/cls)
        return msg.upper()

print(greet("Asha"))
g = Greeter("Asha")
print(g.greet())
print(Greeter.greet(g))   # explicit self

Short version: Methods are functions attached to a class/type, usually operating on that type's data.

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext