Introduction to Python Objects, Classes,
`self`, and Constructors
Introduction
Welcome! Today, we're going to delve into an exciting aspect of Python programming:
Object oriented programming.
These concepts are fundamental to understanding how Python organizes and models data,
making our code more organized and efficient.
Objects and Classes
In Python, everything is an object. An object is a self-contained unit that holds both data and
the methods that operate on that data.
Objects are created using blueprints called classes. A class defines the structure and behavior
of an object.
Defining a Class
To create a class, use the `class` keyword followed by the class name.
Inside the class, you define attributes (data) and methods (functions) that the objects
of this class will have.
Example Class
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return "Woof!"
Here, we've defined a class called `Dog` with attributes `name` and `age`, and a method
`bark`.
The `self` Parameter
In methods within a class, the first parameter is always `self`. It refers to the instance of the
object calling the method.It gives methods access to the object's attributes and other methods.
The `__init__` Constructor
- The `__init__` method is a special method, also known as the constructor. It initializes the
object's attributes when it's created.
- It's automatically called when an object is created from the class.
Creating Objects
To create an object, call the class as if it were a function, passing the required arguments to
the constructor.
Example Object Creation
dog1 = Dog("Buddy", 3)
dog2 = Dog("Max", 5)
Here, we've created two `Dog` objects: `dog1` and `dog2`.
Accessing Object Attributes
You can access an object's attributes using dot notation.
For example, `dog1.name` gives us "Buddy".
Calling Object Methods
Call an object's methods similarly using dot notation.
For instance, `dog2.bark()` returns "Woof!"
Putting it All Together
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
return "Woof!"
dog1 = Dog("Buddy", 3)
dog2 = Dog("Max", 5)
print(dog1.name) # Output: Buddy
print(dog2.bark()) # Output: Woof!
Conclusion
Understanding objects, classes, `self`, and constructors is crucial for organizing code
and creating efficient, modular programs in Python.
Feel free to experiment and create your own classes to model various aspects of your
projects.
Thank you for joining this lecture, and happy coding!
Assignment #23
Question 1: Bank Account Class
Create a Python class called BankAccount that represents a simple bank account. The class
should have attributes account_number, balance, and methods deposit and withdraw. Make
sure to initialize the balance attribute using the constructor. The withdraw method should not
allow withdrawing more than the current balance.
Question 2: Student Class
Create a Python class called Student that represents a student's information. The class should
have attributes name, age, and a list grades to store the student's grades. Implement methods
add_grade to add a grade to the list, get_average_grade to calculate and return the average
grade, and is_passing to check if the student's average grade is above a certain passing
threshold.
Question 3: Product Catalog Class
Create a Python class called Product to represent products in a catalog. The class should have
attributes name, price, and quantity. Implement methods add_stock to increase the quantity of
a product, sell to decrease the quantity when a product is sold, and get_inventory_value to
calculate and return the total value of the available inventory.
Question 4: Information Class
Create a Python class called Information to store personal information. The class should have
attributes name, number, email, and address. Implement a method called display_info that
prints out the information in a structured format, including your original information.