You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

11.3 Instance methods

3 min readjune 24, 2024

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 '' 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

Top images from around the web for Constructor method with parameters
Top images from around the web for Constructor method with parameters
  • [__init__()](https://www.fiveableKeyTerm:__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
    • 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
  • included by assigning
    • Default values specified using
      =
      operator after parameter name (
      def __init__(self, name, age=0):
      )
    • If argument not provided when creating instance, default value used

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 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
  • 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
        increases
        speed
        attribute of instance by
        10
  • Class methods defined using
    [@classmethod](https://www.fiveableKeyTerm:@classmethod)
    decorator and operate on class itself
    • Take
      [cls](https://www.fiveableKeyTerm: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](https://www.fiveableKeyTerm:@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

  • : Instance methods help achieve encapsulation by bundling data and methods that operate on that data within a class
  • : Subclasses inherit instance methods from their parent classes, allowing for code reuse and specialization
  • : Instance methods can be overridden in subclasses, enabling different implementations of the same method name across related classes
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary