Skip to ContentGo to accessibility page

11.3 Instance methods

Learning objectives

By the end of this section you should be able to

  • Create and implement __init__() with multiple parameters including default parameter values.
  • Describe what information an instance method has access to and can modify.

More about __init__()

In Python, __init__() is the special method that creates instances. __init__() must have the calling instance, self, as the first parameter and can have any number of other parameters with or without default parameter values.

Checkpoint

Creating patient vital signs instances

Creating patient vital signs instances; ch 11, video 7

Concepts in Practice

Defining and using __init__() with parameters

Consider the example above.

1.
Suppose the programmer wanted to make blood pressure a required parameter in __init__(). Which is the correct __init__() method header?
  1. def __init__(p_id, bp, self, tmp=-1.0, hr=-1, rr=-1):
  2. def __init__(self, p_id, bp, tmp=-1.0, hr=-1, rr=-1):
  3. def __init__(self, p_id, bp=[-1,-1], tmp=-1.0, hr=-1, rr=-1):
2.
Which is a correct call to __init__() to create an instance with p_id=5241?
  1. patient_10 = Vitals(self, 5241)
  2. patient_10 = Vitals(5241)
  3. both
3.
Suppose another __init__() definition is added after the first with the header as follows:
def __init__(self, p_id, bp, tmp, hr, rr)
What is the impact on the program?
  1. no change.
  2. Second __init__() definition produces an error.
  3. First two __init__() calls produce an error.

Instance methods

An instance method is used to access and modify instance attributes as well as class attributes. All methods shown so far, and most methods defined in a class definition, are instance methods.

Example 11.1

Instance methods are often used to get and set instance information

  class ProductionCar:
    def __init__(self, make, model, year, max_mph = 0.0):
      self.make = make
      self.model = model
      self.year = year
      self.max_mph = max_mph

    def max_kmh(self):
      return self.max_mph * 1.609344

    def update_max(self, speed):
      self.max_mph = speed

  car_1 = ProductionCar('McLaren', 'Speedtail', 2020) # car_1.max_mph is 0.0
  car_1.update_max(250.0) # car_1.max_mph is 250.0
  print(car_1.make, car_1.model, 'reaches', car_1.max_mph, 'mph (',
    car_1.max_kmh(), 'km/h)') # Prints McLaren Speedtail reaches 250.0 mph (402.336 km/h)
  

Concepts in Practice

CoffeeOrder instance methods

Consider the example below:

  class CoffeeOrder:
    loc = 'Cafe Coffee'
    cls_id = 1

    def __init__(self, size=16, milk=False, sugar=False):
      self.order_id = CoffeeOrder.cls_id
      self.cup_size = size
      self.with_milk = milk
      self.with_sugar = sugar
      CoffeeOrder.cls_id += 1

    def change(self, milk, sugar):
      self.with_milk = milk
      self.with_sugar = sugar

    def print_order(self):
      print(CoffeeOrder.loc,'Order', self.order_id, ':', self.cup_size, 'oz')
      if self.with_milk:
        print('\twith milk')
      if self.with_sugar:
        print('\twith sugar')
  
  order_1 = CoffeeOrder(8)
  order_2 = CoffeeOrder(8, True, False)
  order_1.change(False, True)
  
4.
What does order_1 represent at the end of the program?
  1. 8 oz coffee
  2. 8 oz coffee with milk
  3. 8 oz coffee with sugar
5.
Why is change() an instance method?
  1. can change attributes
  2. has multiple parameters
  3. can change an instance
6.
Can print_order() be unindented?
  1. yes
  2. no

Try It

Creating a class for vending machines

Write a class, VendingMachine, as described below. Default values follow the attributes. Ex: count's default value is 0. Create a vending machine using a value read from input and call instance methods.

Instance attributes:

  • count: 0
  • max: 0

Methods:

  • __init__(num): initializes count and max with num parameter
  • refill(): assigns count with max's value and prints "Refilled"
  • sell(order): assigns count with the value of count minus order and prints "Sold: [order]"
  • print_stock(): prints "Current stock: [count]"

Given input:

  100
  25
  

The output is:

  Current stock: 100
  Sold: 25
  Current stock: 75
  Refilled
  Current stock: 100
  
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.