1
Methods in Python
✦ Methods are functions de ned within the scope of a class.
✦ They help to encapsulate the behavior of an object, keeping all related functionality
within the class.
✦ They represent what the object can do and how it interacts with its data (attributes).
• For example: a ShoppingCart class could have an add_item() method to add
an item to the cart.
✦ Instance methods are called using dot notation on an instance of the class.
✦ They can take arguments to customize how they perform speci c tasks.
✦ self is the rst parameter of every instance method. It refers to the instance that the
method is called on and provides access to the instance’s attributes and methods.
✦ Methods can also return values, just like regular functions.
✦ Method names usually contain verbs, since they represent actions performed by the
instances.
✦ In Python, their method names usually follow the snake_case naming convention,
where every word is written in lowercase and words are separated by an
underscore.
• For example: display_name(), calculate_total(), move_forward().
Fig. 1 Instance Method (General Syntax)
Python OOP - Object Oriented Programming for Beginners
fi
fi
fi
2
Fig. 2 Instance Method (Example)
Fig. 3 De ne an Instance and Call a Method on the Instance
Fig. 4 Calling a Method that Takes No Arguments (Assuming that remove_control
is already de ned)
Python OOP - Object Oriented Programming for Beginners
fi
fi