Skip to main content

Object vs Function vs Method


Object vs Function vs Method (Python)

In Python, these three concepts are closely related, which is why they can feel confusing at first. The key idea is who owns what.

1. Function

A function is a reusable block of code that performs a task. It exists independently.

def greet(name):
    return "Hello " + name

greet("Alex")
  • Stands alone
  • Not tied to any object
  • Can be called from anywhere

Think: “Do this task.”

2. Object

An object represents a thing. It holds data (attributes) and behavior (methods).

user = {
    "name": "Alex",
    "age": 25
}

Or more commonly in Python, using a class:

class User:
    pass

u = User()
  • Objects store related data
  • Created from classes
  • Almost everything in Python is an object

Think: “A thing with data.”

3. Method

A method is a function that belongs to an object.

class User:
    def greet(self):
        return "Hello, I'm Alex"

u = User()
u.greet()
  • Method = function + object
  • self refers to the current object

Think: “What this thing can do.”

Same Function, Different Role

A function can become a method when it belongs to a class. The behavior is similar, but the context is different.

def say_hi():
    print("Hi")

say_hi()  # function
class Person:
    def say_hi(self):
        print("Hi")

p = Person()
p.say_hi()  # method

The code may look similar, but the second version belongs to an object.

Comparison Table

Concept What it is Example
Function Standalone action greet()
Object Instance of a class u = User()
Method Function inside a class u.greet()

Real-world Analogy

  • Function → a recipe
  • Object → a kitchen
  • Method → a recipe used by that kitchen

Summary

  • Function: does something
  • Object: holds data
  • Method: does something for an object

In Python:

  • Functions live on their own
  • Methods always take self
  • Objects are created from classes

Object vs Function vs Method (JavaScript)

These three concepts are closely related, which is why they can be confusing at first. The key difference is who owns what.

1. Function

A function is a reusable block of code that performs a task. It exists on its own.

function greet(name) {
  return "Hello " + name;
}

greet("Alex");
  • Stands alone
  • Not tied to any object
  • Can be reused anywhere

Think: “Do this task.”

2. Object

An object represents a thing. It groups data (properties) and behavior (methods) together.

const user = {
  name: "Alex",
  age: 25
};
  • Stores related information
  • Made of key–value pairs
  • Can contain values, functions, arrays, or other objects

Think: “A thing with data.”

3. Method

A method is a function that belongs to an object.

const user = {
  name: "Alex",
  greet() {
    return "Hello, I'm " + this.name;
  }
};

user.greet();
  • Method = function + object
  • Uses the object’s data via this

Think: “What this thing can do.”

Same Function, Different Role

The same function can act as a function or a method depending on context.

function sayHi() {
  console.log("Hi");
}

const obj = {
  sayHi: sayHi
};
  • sayHi() → function
  • obj.sayHi() → method

Comparison Table

Concept What it is Example
Function Standalone action doWork()
Object Data container { name: "Alex" }
Method Function inside an object user.greet()

Real-world Analogy

  • Function → a recipe
  • Object → a kitchen
  • Method → a recipe used by that kitchen

Summary

  • Function: does something
  • Object: holds things
  • Method: does something for an object

Further Reading


People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *