WEEK 13
In Python, a class is a blueprint for creating objects (instances) that encapsulate data
(attributes) and functionality (methods). Objects are instances of a class, which means they
are specific instances that are created based on the structure defined by the class. Let's walk
through the creation of a class and objects with a suitable example:
Example: Creating a Class and Objects
Let's create a simple Person class to represent individuals with attributes like name and age,
and methods to manipulate and display information about each person.
python
Copy code
class Person:
# Constructor method to initialize attributes
def __init__(self, name, age):
[Link] = name
[Link] = age
# Method to introduce the person
def introduce(self):
print(f"Hello, my name is {[Link]} and I am {[Link]} years
old.")
# Creating objects (instances) of the Person class
person1 = Person("Alice", 30)
person2 = Person("Bob", 25)
# Accessing attributes and calling methods of objects
[Link]() # Output: Hello, my name is Alice and I am 30 years
old.
[Link]() # Output: Hello, my name is Bob and I am 25 years old.
Explanation:
1. Class Definition (Person):
o Person is defined using the class keyword followed by the class name.
o Inside the class, we define:
Attributes (name and age): These are initialized using the __init__
method (constructor), which is automatically called when an instance
of the class is created (self refers to the instance being created).
Methods (introduce): These are functions defined inside the class
that can perform operations on the object's data.
2. Constructor (__init__ method):
o The __init__ method is a special method that initializes new objects. It
accepts parameters (name and age in this case) that are used to initialize the
object's attributes ([Link] and [Link]).
3. Objects (Instances):
o person1 and person2 are instances (objects) of the Person class. They are
created by calling the class name as if it were a function, passing the necessary
arguments (name and age).
o Each instance (person1 and person2) has its own set of attributes (name and
age) that are specific to that instance.
4. Accessing Attributes and Calling Methods:
WEEK 13
o Attributes (name and age) of each object can be accessed using dot notation
([Link]).
o Methods (like introduce) of each object are called using dot notation
([Link]()), and they can operate on the object's attributes.
Additional Notes:
Self: In Python, self is a convention (not a keyword) used to refer to the instance of
the class. It must be the first parameter in any method definition within the class,
including __init__ and other methods.
Encapsulation: The Person class encapsulates related data (attributes) and behavior
(methods) into a single unit, making it easier to manage and reuse in your code.
Instance Variables: Attributes like [Link] and [Link] are instance variables,
which means they are specific to each instance of the class.
Class vs. Instance: The class (Person) defines the structure and behavior, while
instances (person1, person2, etc.) are specific objects created from that structure.
Conclusion:
Creating classes and objects in Python allows you to model real-world entities, encapsulate
data and behavior, and facilitate code organization and reuse. Understanding how to define
classes, create objects, and interact with them using attributes and methods is fundamental to
object-oriented programming in Python.