Python Object Methods — Complete Cheat Sheet
What Are Object Methods?
Object methods are functions defined inside a class that represent the behavior of an object. They always take self as the first parameter.
When an object calls a method, self refers to that specific object.
Basic Object Method
A simple class containing a method:
class Person:
def greet(self):
print("Hello!")p = Person() p.greet()
Here, greet() is an object method. The object pcalls it using p.greet().
What is the "self" Parameter?
self refers to the current calling object. It gives access to:
- Object attributes
- Other object methods
- Data stored inside the instance
class Car:
def show(self):
print("This is a car object:", self)The object reference is passed automatically when calling a method.
Methods That Use Object Attributes
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def details(self):
print("Name:", self.name)
print("Age:", self.age)s = Student("Emil", 21)
s.details()details() displays the object’s stored data via self.
Object Methods with Parameters
Methods can accept additional arguments:
class MathTool:
def multiply(self, a, b):
return a * btool = MathTool() print(tool.multiply(4, 5))
You only pass a and b; Python automatically passes the object as self.
Calling One Method From Another
class Person:
def greet(self):
print("Hello")
def introduce(self):
self.greet()
print("I am a person")p = Person() p.introduce()
Use self.method_name() to call other methods inside the class.
Summary of Object Method Concepts
| Concept | Meaning |
|---|---|
| self | Refers to the calling object |
| Method | Function inside a class |
| object.method() | Calling a method using an object |
| self.attribute | Access or modify object data |
| self.other_method() | Call another method inside the class |