Reading Object Classes
Reading Object Classes
Objective:
By the end of this reading, you should be able to:
Python is an object-oriented programming (OOP) language, which means it uses a paradigm centered around
objects and classes. Here's an explanation of these fundamental concepts:
Classes:
A class is a blueprint or template for creating objects. It defines the structure and behavior that its objects will
have.
Think of a class as a cookie cutter, and objects as the cookies cut from that template.
Creating Classes:
When you create a class, you specify the attributes(data) and methods (functions) that objects of that class will
have.
Attributes are defined as variables within the class, and methods are defined as functions.
For example,you can design a "Car" class with attributes such as "color" and "speed," along with methods like
"accelerate."
Objects:
State:
about:blank 1/9
9/28/23, 1:54 PM about:blank
The attributes or data that describe the object. For our "Car" object, this might include attributes like "color",
"speed", and "fuel level".
Behavior:
The actions or methods that the object can perform. In Python, methods are functions that belong to objects and
can change the object's state or perform specific operations.
Instantiating Objects:
Once you've defined a class, you can create individual objects (instances) based on that class.
Each object is independent and has its own set of attributes and methods.
To create an object, you use the class name followed by parentheses, so: "my_car = Car()"
You interact with objects by calling their methods or accessing their attributes using dot notation.
For example, if you have a Car object named my_car, you can set its color with my_car.color = "blue" and
accelerate it with my_car.accelerate() if there's an accelerate method defined in the class.
Please don't directly copy and use this code as it's meant as a template for explanation and isn't
tailored for specific results.
1. 1
1. class ClassName:
Copied!
Class attributes are variables that are shared among all instances (objects) of the class.
They are defined within the class but outside of any methods.
1. 1
2. 2
3. 3
1. class ClassName:
2. # Class attributes (shared by all instances)
3. class_attribute = value
Copied!
about:blank 2/9
9/28/23, 1:54 PM about:blank
1. class ClassName:
2. # Class attributes (shared by all instances)
3. class_attribute = value
4.
5. # Constructor method (initialize instance attributes)
6. def __init__(self, attribute1, attribute2, ...):
7. pass
8. # ...
Copied!
Instance attributes are variables that store data specific to each instance of the class.
They are initialized within the init method using the self keyword followed by the attribute name.
These attributes hold unique data for each object created from the class.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
1. class ClassName:
2. # Class attributes (shared by all instances)
3. class_attribute = value
4.
5. # Constructor method (initialize instance attributes)
6. def __init__(self, attribute1, attribute2, ...):
7. self.attribute1 = attribute1
8. self.attribute2 = attribute2
9. # ...
Copied!
about:blank 3/9
9/28/23, 1:54 PM about:blank
The self parameter is required in instance methods, allowing them to access instance attributes and call
other methods within the class.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
1. class ClassName:
2. # Class attributes (shared by all instances)
3. class_attribute = value
4.
5. # Constructor method (initialize instance attributes)
6. def __init__(self, attribute1, attribute2, ...):
7. self.attribute1 = attribute1
8. self.attribute2 = attribute2
9. # ...
10.
11. # Instance methods (functions)
12. def method1(self, parameter1, parameter2, ...):
13. # Method logic
14. pass
15.
Copied!
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
16. 16
17. 17
18. 18
1. class ClassName:
2. # Class attributes (shared by all instances)
3. class_attribute = value
4.
5. # Constructor method (initialize instance attributes)
6. def __init__(self, attribute1, attribute2, ...):
about:blank 4/9
9/28/23, 1:54 PM about:blank
7. self.attribute1 = attribute1
8. self.attribute2 = attribute2
9. # ...
10.
11. # Instance methods (functions)
12. def method1(self, parameter1, parameter2, ...):
13. # Method logic
14. pass
15.
16. def method2(self, parameter1, parameter2, ...):
17. # Method logic
18. pass
Copied!
To create objects (instances) of the class, you call the class like a function and provide arguments required
by the constructor.
Each object is a distinct instance of the class, with its own set of instance attributes and the ability to call
methods defined in the class.
1. 1
2. 2
3. 3
Copied!
In this section we will call methods on objects, specifically object1 and object2.
The methods method1 and method2 are defined in the ClassName class, and you're calling them on
object1 and object2 respectively.
You pass values param1_value and param2_value as arguments to these methods. These arguments are
used within the method's logic.
This is the most straightforward way to call an object's method. In this we use the dot notation
(object.method()) to directly invoke the method on the object.
For example, result1 = object1.method1(param1_value, param2_value, ...) calls method1 on object1.
1. 1
2. 2
3. 3
4. 4
about:blank 5/9
9/28/23, 1:54 PM about:blank
Copied!
Here's an alternative way to call an object's method by assigning the method reference to a variable.
method_reference = object1.method1 assigns the method method1 of object1 to the variable
method_reference.
Later, we call the method using the variable like this: result3 = method_reference(param1_value,
param2_value, …).
1. 1
2. 2
3. 3
Copied!
1. 1
2. 2
Copied!
1. 1
2. 2
Copied!
Finally,we access a class attribute, which is shared by all instances of the class.
class_attr_value = ClassName.class_attribute accesses the class attribute class_attribute from the
ClassName class and assigns its value to the variable
class_attr_value.
1. 1
2. 2
about:blank 6/9
9/28/23, 1:54 PM about:blank
1. # Accessing class attributes (shared by all instances)
2. class_attr_value = ClassName.class_attribute
Copied!
Real-world example
Let's write a python program that simulates a simple car class, allowing you to create car instances, accelerate
them, and display their current speeds.
1. Let's start by defining a Car class that includes the following attributes and methods:
Constructor method __init__ that takes parameters for the car's make, model, color, and an optional
speed (defaulting to 0). This method initializes instance attributes for make, model, color, and speed.
Method accelerate(self, acceleration) that allows the car to accelerate. If the acceleration does not
exceed the max_speed, update the car's speed attribute. Otherwise, set the speed to the max_speed.
1. class Car:
2. # Class attribute (shared by all instances)
3. max_speed = 120 # Maximum speed in km/h
4.
5. # Constructor method (initialize instance attributes)
6. def __init__(self, make, model, color, speed=0):
7. self.make = make
8. self.model = model
9. self.color = color
10. self.speed = speed # Initial speed is set to 0
11.
12. # Method for accelerating the car
13. def accelerate(self, acceleration):
14. if self.speed + acceleration <= Car.max_speed:
15. self.speed += acceleration
16. else:
17. self.speed = Car.max_speed
about:blank 7/9
9/28/23, 1:54 PM about:blank
18.
19. # Method to get the current speed of the car
20. def get_speed(self):
21. return self.speed
Copied!
2. Now, we will instantiate two objects of the Car class, each with the following characteristics:
Copied!
3. Using the accelerate method, we will increase the speed of car1 by 30 km/h and car2 by 20 km/h.
1. 1
2. 2
3. 3
4. 4
1.
2. # Accelerate the cars
3. car1.accelerate(30)
4. car2.accelerate(20)
Copied!
4. Lastly, we will display the current speed of each car by utilizing the `get_speed method.
1. 1
2. 2
3. 3
4. 4
1.
2. # Print the current speeds of the cars
3. print(f"{car1.make} {car1.model} is currently at {car1.get_speed()} km/h.")
4. print(f"{car2.make} {car2.model} is currently at {car2.get_speed()} km/h.")
Copied!
Next Steps
In conclusion, this reading provides a fundamental understanding of objects and classes in Python, essential
concepts in object-oriented programming. Classes serve as blueprints for creating objects, encapsulating both
data attributes and methods. Objects represent real-world entities and possess their own unique state and
behavior. The structured code example presented in the reading outlines the key elements of a class, including
class attributes, the constructor method for initializing instance attributes, and instance methods for defining
object-specific functionality.
about:blank 8/9
9/28/23, 1:54 PM about:blank
You can apply the concepts of objects and classes in the upcoming laboratory session to gain hands-on
experience.
Author
Akansha Yadav
Changelog
Date Version Changed by Change Description
2023-09-05 1.0 Akansha Yadav Created reading
about:blank 9/9