Fiveable

🐍Intro to Python Programming Unit 11 Review

QR code for Intro to Python Programming practice questions

11.3 Instance methods

11.3 Instance methods

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
🐍Intro to Python Programming
Unit & Topic Study Guides

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 of name parameter to name attribute of instance
  • 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
Constructor method with parameters, Magic Methods in Python

Instance methods and attribute access

  • Instance methods access and modify instance attributes using self keyword
    • self.attribute_name refers to attribute of current instance
    • Modifying self.attribute_name changes value of attribute for that specific instance
      • self.name = "New Name" changes name 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 class
      • Car.wheels = 5 changes wheels attribute for all instances of Car 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
Constructor method with parameters, Python: Difference between class and instance attributes - Stack Overflow

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 increases speed attribute of instance by 10
  • 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
  • Static methods defined using @staticmethod decorator and do not have access to instance or class attributes
    • Do not take self or cls 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

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
Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly → and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot

2,589 studying →