Instance methods are fundamental to Python classes, allowing objects to interact with their own data. They define behaviors specific to each instance, accessing and modifying attributes using the 'self' keyword.
These methods come in various types, including constructors, regular instance methods, class methods, and static methods. Each serves a unique purpose, from initializing objects to performing operations on the class itself or providing utility functions.
Instance Methods in Python Classes
Constructor method with parameters
__init__()
special instance method called when creating new instance of class- Initializes attributes of instance
- First parameter always
self
refers to instance being created
- Takes multiple parameters to initialize instance attributes
- Parameters defined after
self
separated by commas - Each parameter assigned to instance attribute using
self.attribute_name = parameter_name
self.name = name
assigns value ofname
parameter toname
attribute of instance
- Parameters defined after
- Optional parameters included by assigning default values
- Default values specified using
=
operator after parameter name (def __init__(self, name, age=0):
) - If argument not provided when creating instance, default value used
- Default values specified using
Instance methods and attribute access
- Instance methods access and modify instance attributes using
self
keywordself.attribute_name
refers to attribute of current instance- Modifying
self.attribute_name
changes value of attribute for that specific instanceself.name = "New Name"
changesname
attribute of instance to"New Name"
- Can also access class attributes directly using class name
ClassName.attribute_name
refers to class attribute- Modifying
ClassName.attribute_name
changes value of attribute for all instances of classCar.wheels = 5
changeswheels
attribute for all instances ofCar
class
- If instance attribute has same name as class attribute, accessing
self.attribute_name
refers to instance attribute, overriding class attribute for that instance - Method invocation occurs when an instance method is called on an object
Types of class methods
- Instance methods defined within class and operate on individual instances
- Take
self
as first parameter referring to instance on which method is called - Access and modify instance attributes using
self
def drive(self): self.speed += 10
increasesspeed
attribute of instance by10
- Take
- Class methods defined using
@classmethod
decorator and operate on class itself- Take
cls
as first parameter referring to class - Access and modify class attributes using
cls
@classmethod def from_string(cls, car_str): ...
creates instance of class from string representation
- Often used for alternative constructors or methods that operate on class as a whole
- Take
- Static methods defined using
@staticmethod
decorator and do not have access to instance or class attributes- Do not take
self
orcls
as parameters - Independent of instances and class, used for utility functions or operations that don't require access to instance or class data
@staticmethod def miles_to_kilometers(miles): return miles 1.60934
converts miles to kilometers without needing instance or class
- Do not take
Object-Oriented Programming Concepts
- Encapsulation: Instance methods help achieve encapsulation by bundling data and methods that operate on that data within a class
- Inheritance: Subclasses inherit instance methods from their parent classes, allowing for code reuse and specialization
- Polymorphism: Instance methods can be overridden in subclasses, enabling different implementations of the same method name across related classes