A class in Python is a blueprint for creating objects. It defines attributes (variables) and methods (functions) that the created objects will have. Think of a class as a blueprint for building a house. It defines the structure and features of the house but isn't an actual house by itself.
An object is an instance of a class. When you create an object from a class, you are essentially making a concrete realization of that class. Each object can have its own unique set of attributes and can use the methods defined in the class. An object is a house built from that blueprint. Each house (object) can have its own unique features, like paint color or number of rooms, but all houses built from the same blueprint share the same basic structure.
Step-by-Step Example
Step 1: Define a Class
First, we define a class called Dog. This is our blueprint for creating dog objects.
class Dog: def __init__(self, name, age): self.name = name # Attribute self.age = age # Attribute def bark(self): # Method print("Woof!") |
In this example:
- __init__: This is a special method called a constructor. It initializes the attributes of the dog.
- self: A reference to the current instance of the class. It is used to access attributes and methods of the object.
- name and age: Attributes of the dog.
- bark: A method that makes the dog bark.
The __init__() Function
The __init__() function is a special function in Python classes. It's called automatically when you create a new object from a class. This function is used to initialize the object's attributes and to perform any setup operations necessary for the object.
Think of __init__() as a way to set up your object with initial values and configurations as soon as it's created.
The self Parameter
The self parameter is a reference to the current instance of the class. It allows you to access the attributes and methods of the class within its methods. While it is traditionally named self, you can call it anything you like. However, it must be the first parameter of any method in the class.
Why Use self?
Using self helps to differentiate between instance variables (those belonging to the object) and local variables or parameters. It ensures that the attributes and methods are associated with the particular instance of the class.
Step 2: Create an Object
Now, we create an object (an instance of the Dog class) called my_dog.
my_dog = Dog("Buddy", 3) |
Here, my_dog is an instance of the Dog class with the name "Buddy" and age 3.
Step 3: Use the Object
We can access the attributes and methods of my_dog using dot notation.
print(my_dog.name) # Output: Buddy print(my_dog.age) # Output: 3 my_dog.bark() # Output: Woof! |
Putting It All Together
Here's the complete code for our simple example:
class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): print("Woof!") # Create an object of the Dog class my_dog = Dog("Buddy", 3) # Access attributes and methods print(my_dog.name) # Output: Buddy print(my_dog.age) # Output: 3 my_dog.bark() # Output: Woof! |
To summarize:
- Class: Defines a blueprint for creating objects (e.g., Dog).
- Object: An instance of a class (e.g., my_dog).
- Attributes: Characteristics of the object (e.g., name, age).
- Methods: Functions that the object can perform (e.g., bark).