objetos y clases modulo 3
objetos y clases modulo 3
Objectives
In this reading, you will learn about:
Python is an object-oriented programming (OOP) language that uses a paradigm centered around objects and classes.
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
The attributes or data that describe the object. For your "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 because it is a template for explanation and not for specific results.
about:blank 1/6
28/8/24, 9:02 about:blank
1. 1
1. class ClassName:
Copied!
Class attributes are variables shared among all class instances (objects).
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!
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
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 class instance.
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 2/6
28/8/24, 9:02 about:blank
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
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
Copied!
Using the same steps you can define multiple instance methods.
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, ...):
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 the constructor requires.
Each object is a distinct instance of the class, with its own instance attributes and the ability to call methods defined in the class.
1. 1
2. 2
3. 3
Copied!
In this section, you 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.
about:blank 3/6
28/8/24, 9:02 about:blank
This is the most straightforward way to call an object's method. In this, use the dot notation (object.method()) to invoke the method on the object directly.
For example, result1 = object1.method1(param1_value, param2_value, ...) calls method1 on object1.
1. 1
2. 2
3. 3
4. 4
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, 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!
1. 1
2. 2
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.
about:blank 4/6
28/8/24, 9:02 about:blank
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
19. 19
20. 20
21. 21
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
18.
19. # Method to get the current speed of the car
20. def get_speed(self):
21. return self.speed
Copied!
2. Now, you will instantiate two objects of the Car class, each with the following characteristics:
Copied!
3. Using the accelerate method, you will increase the speed of car1 by 30 km/h and car2 by 20 km/h.
1. 1
2. 2
3. 3
Copied!
4. Lastly, you will display the current speed of each car by utilizing the get_speed method.
1. 1
2. 2
3. 3
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 data attributes and methods. Objects represent real-world entities and possess their 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 5/6
28/8/24, 9:02 about:blank
In the upcoming laboratory session, you can apply the concepts of objects and classes to gain hands-on experience.
Author
Akansha Yadav
about:blank 6/6