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
selfrefers to instance being created
- Takes multiple parameters to initialize instance attributes
- Parameters defined after
selfseparated by commas - Each parameter assigned to instance attribute using
self.attribute_name = parameter_nameself.name = nameassigns value ofnameparameter tonameattribute 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
selfkeywordself.attribute_namerefers to attribute of current instance- Modifying
self.attribute_namechanges value of attribute for that specific instanceself.name = "New Name"changesnameattribute of instance to"New Name"
- Can also access class attributes directly using class name
ClassName.attribute_namerefers to class attribute- Modifying
ClassName.attribute_namechanges value of attribute for all instances of classCar.wheels = 5changeswheelsattribute for all instances ofCarclass
- If instance attribute has same name as class attribute, accessing
self.attribute_namerefers 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
selfas first parameter referring to instance on which method is called - Access and modify instance attributes using
selfdef drive(self): self.speed += 10increasesspeedattribute of instance by10
- Take
- Class methods defined using
@classmethoddecorator and operate on class itself- Take
clsas 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
@staticmethoddecorator and do not have access to instance or class attributes- Do not take
selforclsas 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.60934converts 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