Learning objectives
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.
Concepts in Practice
Classes and 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()
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.
Concepts in Practice
instances and __init__()
Consider the example below:
1 | class Rectangle: |
2 | def __init__(self): |
3 | self.length = 1 |
4 | self.width = 1 |
5 | def area(self): |
6 | return self.length * self.width |
7 | |
8 | room_1 = Rectangle() |
9 | room_1.length = 10 |
10 | room_1.width = 15 |
11 | print("Room 1's area:", room_1.area()) |
12 | room_3 = Rectangle() |
13 | room_3.length = 12 |
14 | room_3.width = 14 |
15 | print("Room 3's area:", room_3.area()) |
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
.
Concepts in Practice
Instances and class attributes
Consider the example above.
Try It
Creating a class for an airline's flight tickets
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
Try It
Creating a class for fantasy books
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