Lecture 6: Class and objects
class, object, class vs instance variables
Class and Objects
Let's think about a few more classes and their attributes
In Python, classes and objects are fundamental concepts of object-oriented programming
(OOP). Let's go through the basics of creating classes and objects in Python.
Classes: A class is a blueprint for creating objects. It defines a data structure that includes
attributes (variables) and methods (functions). To create a class in Python, you use the class
keyword.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def display_info(self):
print(f"{self.year} {self.make} {self.model}")
In this example, we've created a Car class with three attributes (make, model, and year) and a
method (display_info) to display information about the car.
Objects: An object is an instance of a class. You can create multiple objects from the same class,
each with its own set of attributes and methods.
# Creating objects of the Car class
car1 = Car("Toyota", "Camry", 2022)
car2 = Car("Honda", "Civic", 2023)
# Accessing attributes
print(car1.make) # Output: Toyota
print(car2.year) # Output: 2023
# Calling methods
car1.display_info() # Output: 2022 Toyota Camry
car2.display_info() # Output: 2023 Honda Civic
In this example, car1 and car2 are objects of the Car class. Each object has its own values for
the make, model, and year attributes.
Constructor (__init__ method): The __init__ method is a special method in a class that is
called when an object is created. It is used to initialize the attributes of the object.
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
The self parameter is a reference to the instance of the class and is used to access its attributes
and methods.
Accessing Attributes and Calling Methods: You can access the attributes of an object using dot
notation (object.attribute). Similarly, you can call the methods of an object using dot
notation (object.method()).
print(car1.make) # Accessing attribute
car2.display_info() # Calling method
This is a basic introduction to classes and objects in Python. As you become more familiar with
OOP concepts, you can explore concepts like inheritance, encapsulation, and polymorphism.
Instance vs Class Variables
Instance variables and class variables are two types of variables used in Python classes. They
serve different purposes and have distinct behaviors:
Instance Variables:
Instance variables are unique to each instance (object) of a class. They store data that is specific
to an instance and can have different values for different instances. Instance variables are
defined within the constructor (__init__ method) of the class using the self keyword.
class Student:
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age # Instance variable
student1 = Student("Alice", 20)
student2 = Student("Bob", 22)
print(student1.name) # Output: Alice
print(student2.name) # Output: Bob
In this example, name and age are instance variables unique to each Student object.
Class Variables:
Class variables are shared among all instances of a class. They are defined within the class but
outside any methods. Class variables store data that is common to all instances of the class.
Changes made to class variables affect all instances of the class.
class BankAccount:
interest_rate = 0.03 # Class variable
def __init__(self, balance):
self.balance = balance # Instance variable
account1 = BankAccount(1000)
account2 = BankAccount(1500)
print(account1.interest_rate) # Output: 0.03
print(account2.interest_rate) # Output: 0.03
print(account1.balance) # Output: 1000
print(account2.balance) # Output: 1500
In this example, interest_rate is a class variable shared by both account1 and account2
instances of the BankAccount class.
When to Use Each:
• Use instance variables when you need data specific to each instance of a class.
• Use class variables when you want to share data among all instances of a class or when
the data is common to the entire class.
Keep in mind that modifying a class variable affects all instances of the class. If you want to
modify an instance variable, it only affects the specific instance it belongs to.
Remember that class variables can be accessed both through class names
(ClassName.variable_name) and instance names (instance_name.variable_name).
However, instance variables should typically be accessed through instances
(self.variable_name) within methods.