11.2: Classes and Instances
( \newcommand{\kernel}{\mathrm{null}\,}\)
By the end of this section you should be able to
- Create a class with instance attributes, class attributes, and the
__init__()
method. - Use a class definition to create class instances to represent objects.
Classes and instances
In the previous section, a real-world entity, like a person's social media profile, was modeled as a single object. How could a programmer develop a software system that manages millions of profiles? A blueprint that defines the fields and procedures of a profile would be crucial.
In a Python program, a class defines a type of object with attributes (fields) and methods (procedures). A class is a blueprint for creating objects. Individual objects created of the class type are called instances.
Consider the example below:
class Cat:
def __init__(self):
self.name = 'Kitty'
self.breed = 'domestic short hair'
self.age = 1
def print_info(self):
print(self.name, 'is a ', self.age, 'yr old', self.breed)
pet_1 = Cat()
pet_2 = Cat()
class Cat
self
Creating instances with __init__()
___init___()
is a special method that is called every time a new instance of a class is created. self refers to the instance of a class and is used in class methods to access the specific instance that called the method. __init__()
uses self
to define and initialize the instance's attributes.
Consider the example below:
1 |
Instance attributes vs. class attributes
The attributes shown so far have been instance attributes. An instance attribute is a variable that is unique to each instance of a class and is accessed using the format instance_name.attribute_name
. Another type of attribute, a class attribute, belongs to the class and is shared by all class instances. Class attributes are accessed using the format class_name.attribute_name
.
Consider the example above.
order_id
order_3
Write a class, FlightTicket
, as described below. Default values follow the attributes. Then create a flight ticket and assign each instance attribute with values read from input.
Instance attributes:
- flight_num: 1
- airport: JFK
- gate: T1-1
- time: 8:00
- seat: 1A
- passenger: unknown
Class attributes:
- airline: Oceanic Airlines
- airline_code: OA
Method:
__init__()
: initializes the instance attributesprint_info()
: prints ticket information (provided in template)
Given input:
2121 KEF D22B 11:45 12B Jules Laurent
The output is:
Passenger Jules Laurent departs on flight # 2121 at 11:45 from KEF D22B in seat 12B
Write a class, Book
, as described below. Then create two instances and assign each instance attribute with values read from input.
Instance attributes:
- title: ''
- author: ''
- year: 0
- pages: 0
Class attribute:
- imprint: Fantasy Tomes
Method:
__init__()
: initializes the instance attributesprint_info()
: prints book information (provided in template)
Given input:
Lord of the Bracelets Blake R. R. Brown 1999 423 A Match of Thrones Terry R. R. Thomas 2020 761
The output is:
Lord of the Bracelets by Blake R. R. Brown published by Fantasy Tomes in 1999 with 423 pages A Match of Thrones by Terry R. R. Thomas published by Fantasy Tomes in 2020 with 761 pages