In this tutorial, we will explore how to define classes and create objects in Python, a fundamental aspect of Object-Oriented Programming (OOP). By the end of this tutorial, you will have a clear understanding of what classes are, how to define them, and how to create and interact with their objects.
You will learn how to:
- Define a class in Python
- Create objects from a class
- Understand member variables and methods
In Python, a class serves as a blueprint for creating objects. It is a user-defined prototype which includes a set of attributes that characterize any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
An object is an instance of a class. When a class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated until you create objects.
You can define a class in Python using the keyword class
. The syntax is as follows:
class ClassName:
# Class body
Once a class is defined, you can create objects of the class using the following syntax:
object_name = ClassName()
Member variables are variables that are available across methods in a class. We can also call them as class variables.
A method in python is somewhat similar to a function, except it is associated with object/classes. Methods in python are very similar to functions except for two major differences:
- The method is implicitly used for an object for which it is called.
- The method is accessible to data that is contained within the class.
Let's define a class Person
and create two objects person1
and person2
.
class Person:
pass
person1 = Person()
person2 = Person()
print(person1)
print(person2)
This will output:
<__main__.Person object at 0x7f3150a6c8d0>
<__main__.Person object at 0x7f3150a6c8e0>
Now, let's add some member variables and methods to our Person
class.
class Person:
# A class variable
species = "Homo Sapiens"
# Initializer / Instance attributes
def __init__(self, name, age):
self.name = name
self.age = age
# instance method
def introduction(self):
return f"My name is {self.name} and I am {self.age} years old."
# create objects
person1 = Person("John Doe", 30)
person2 = Person("Jane Doe", 25)
# access variables and methods
print(person1.species)
print(person1.introduction())
print(person2.species)
print(person2.introduction())
This will output:
Homo Sapiens
My name is John Doe and I am 30 years old.
Homo Sapiens
My name is Jane Doe and I am 25 years old.
In this tutorial, we learned how to define classes and create objects in Python. We also covered the concept of member variables and methods. This is a fundamental aspect of Object-Oriented Programming (OOP) in Python.
To continue your learning, you might want to explore topics like class inheritance, method overriding, and other OOP concepts in Python.
Define a class Car
with two member variables: brand
and model
, and a member method display_car()
, which returns a string in the format: "This car is a {brand} {model}".
Create three objects of the class Car
and print the output of display_car()
for each object.
# Solution to Exercise 1
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_car(self):
return f"This car is a {self.brand} {self.model}"
# Solution to Exercise 2
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
car3 = Car("Ford", "Mustang")
print(car1.display_car())
print(car2.display_car())
print(car3.display_car())
The output will be:
This car is a Toyota Corolla
This car is a Honda Civic
This car is a Ford Mustang
Keep practicing to enhance your understanding of classes and objects in Python. Happy learning!