-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
5.1.1 Defining classes and creating objects
In Python, classes are used to define the blueprint of an object, while objects are instances of these classes. A class can have attributes (variables) and methods (functions), and objects created from a class can access these attributes and methods.
Defining a Class
To define a class in Python, use the class keyword followed by the class name. By convention, class names are written in CamelCase (e.g., Person, Car, Dog).
The class body contains:
- Attributes: These are the variables that hold data for an object.
- Methods: These are the functions that define the behavior of the object.
The __init__ Method (Constructor)
The __init__ method is a special method in Python classes. It's called when an object is created from the class. This method initializes the object's attributes.
Example: Defining a Simple Class
# Defining the class class Dog: # Constructor (initializer) def __init__(self, name, breed): self.name = name # Instance attribute self.breed = breed # Instance attribute # Method to display dog info def bark(self): print(f"{self.name} is barking!") def display_info(self): print(f"{self.name} is a {self.breed}") # Creating objects (instances) of the class dog1 = Dog("Buddy", "Golden Retriever") dog2 = Dog("Bella", "Labrador") # Accessing object methods dog1.bark() # Output: Buddy is barking! dog2.display_info() # Output: Bella is a Labrador
In this example:
- Dog is the class.
- __init__(self, name, breed) initializes the name and breed attributes when a Dog object is created.
- bark() and display_info() are methods that operate on the object's attributes.
- dog1 and dog2 are objects (instances of the Dog class).
Creating Objects (Instances)
Objects are created by calling the class as if it were a function. When you call a class to create an object, the __init__ method is automatically executed.
Example: Creating Objects
# Creating an object (instance) of the Dog class dog1 = Dog("Buddy", "Golden Retriever") # Accessing attributes of the object print(dog1.name) # Output: Buddy print(dog1.breed) # Output: Golden Retriever
Here, dog1 is an object of the Dog class. You can access its attributes (name and breed) directly.
Class Attributes vs. Instance Attributes
- Instance Attributes: These are specific to each object created from the class. In the example above, name and breed are instance attributes, as each Dog object has its own values for these attributes.
- Class Attributes: These are shared across all objects of the class. If you want to define an attribute that is the same for all objects, you define it outside of the __init__ method but within the class.
Example: Class Attribute
class Dog: species = "Canine" # Class attribute def __init__(self, name, breed): self.name = name # Instance attribute self.breed = breed # Instance attribute # Creating objects dog1 = Dog("Buddy", "Golden Retriever") dog2 = Dog("Bella", "Labrador") # Accessing class attribute print(dog1.species) # Output: Canine print(dog2.species) # Output: Canine
In this example, species is a class attribute, and all Dog objects will share the same value for it (Canine).
Accessing Methods
Methods in a class define the behavior of the objects. You can access a method by calling it on the object, as shown in the bark() and display_info() examples above.
dog1.bark() # Calling the method to make dog1 bark
Methods typically accept self as their first argument, which refers to the current object that the method is being called on.
Conclusion
- Classes define the structure and behavior of objects in Python.
- Objects are instances of these classes, created by calling the class as a function.
- Each object has its own set of instance attributes and can use the class methods.
- The __init__ method (constructor) is automatically called when an object is instantiated, and it sets the initial values of the object's attributes.
Commenting is not enabled on this course.