Instance methods are functions that belong to objects and can access/modify the object's data through the self parameter.
- First parameter of every instance method
- Refers to the current object
- Python automatically passes it when you call the method
class Example:
def __init__(self, value):
self.value = value
def get_value(self): # self is automatic
return self.value- Getters: Return object data
- Setters: Modify object data
- Actions: Perform operations that may change state
- Queries: Answer questions about the object
- Methods should have a single, clear purpose
- Use descriptive names that indicate what the method does
- Consider whether the method should return a value or modify state
- Keep methods focused and concise
- Always include
selfas the first parameter - Use
self.attributeto access instance variables - Methods can call other methods using
self.method_name() - Consider returning
selffor method chaining