- Python Classes and Objects
- Create a Class
- Example
- Create Object
- Example
- The __init__() Function
- Example
- The __str__() Function
- Example
- Example
- Object Methods
- Example
- The self Parameter
- Example
- Modify Object Properties
- Example
- Delete Object Properties
- Example
- Delete Objects
- Example
- The pass Statement
- Python Objects and Classes
- Python Classes
- Define Python Class
- Python Objects
- Access Class Attributes Using Objects
- Example 1: Python Class and Objects
- Create Multiple Objects of Python Class
- Python Methods
- Table of Contents
Python Classes and Objects
Almost everything in Python is an object, with its properties and methods.
A Class is like an object constructor, or a «blueprint» for creating objects.
Create a Class
To create a class, use the keyword class :
Example
Create a class named MyClass, with a property named x:
Create Object
Now we can use the class named MyClass to create objects:
Example
Create an object named p1, and print the value of x:
The __init__() Function
The examples above are classes and objects in their simplest form, and are not really useful in real life applications.
To understand the meaning of classes we have to understand the built-in __init__() function.
All classes have a function called __init__(), which is always executed when the class is being initiated.
Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
Example
Create a class named Person, use the __init__() function to assign values for name and age:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
Note: The __init__() function is called automatically every time the class is being used to create a new object.
The __str__() Function
The __str__() function controls what should be returned when the class object is represented as a string.
If the __str__() function is not set, the string representation of the object is returned:
Example
The string representation of an object WITHOUT the __str__() function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
Example
The string representation of an object WITH the __str__() function:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
Object Methods
Objects can also contain methods. Methods in objects are functions that belong to the object.
Let us create a method in the Person class:
Example
Insert a function that prints a greeting, and execute it on the p1 object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print(«Hello my name is » + self.name)
p1 = Person(«John», 36)
p1.myfunc()
Note: The self parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.
The self Parameter
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class:
Example
Use the words mysillyobject and abc instead of self:
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print(«Hello my name is » + abc.name)
p1 = Person(«John», 36)
p1.myfunc()
Modify Object Properties
You can modify properties on objects like this:
Example
Delete Object Properties
You can delete properties on objects by using the del keyword:
Example
Delete the age property from the p1 object:
Delete Objects
You can delete objects by using the del keyword:
Example
The pass Statement
class definitions cannot be empty, but if you for some reason have a class definition with no content, put in the pass statement to avoid getting an error.
Python Objects and Classes
In the last tutorial, we learned about Python OOP. We know that python also supports the concept of objects and classes.
An object is simply a collection of data (variables) and methods (functions). Similarly, a class is a blueprint for that object.
Before we learn about objects, let’s first know about classes in Python.
Python Classes
A class is considered as a blueprint of objects. We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. House is the object.
Since many houses can be made from the same description, we can create many objects from a class.
Define Python Class
We use the class keyword to create a class in Python. For example,
class ClassName: # class definition
Here, we have created a class named ClassName .
class Bike: name = "" gear = 0
- Bike — the name of the class
- name/gear — variables inside the class with default values «» and 0 respectively.
Note: The variables inside a class are called attributes.
Python Objects
An object is called an instance of a class. For example, suppose Bike is a class then we can create objects like bike1 , bike2 , etc from the class.
Here’s the syntax to create an object.
# create class class Bike: name = "" gear = 0 # create objects of class bike1 = Bike()
Here, bike1 is the object of the class. Now, we can use this object to access the class attributes.
Access Class Attributes Using Objects
We use the . notation to access the attributes of a class. For example,
# modify the name attribute bike1.name = "Mountain Bike" # access the gear attribute bike1.gear
Here, we have used bike1.name and bike1.gear to change and access the value of name and gear attribute respectively.
Example 1: Python Class and Objects
# define a class class Bike: name = "" gear = 0 # create object of class bike1 = Bike() # access attributes and assign new values bike1.gear = 11 bike1.name = "Mountain Bike" print(f"Name: , Gears: ")
Name: Mountain Bike, Gears: 11
In the above example, we have defined the class named Bike with two attributes: name and gear .
We have also created an object bike1 of the class Bike .
Finally, we have accessed and modified the attributes of an object using the . notation.
Create Multiple Objects of Python Class
We can also create multiple objects from a single class. For example,
# define a class class Employee: # define an attribute employee_id = 0 # create two objects of the Employee class employee1 = Employee() employee2 = Employee() # access attributes using employee1 employee1.employeeID = 1001 print(f"Employee ID: ") # access attributes using employee2 employee2.employeeID = 1002 print(f"Employee ID: ")
Employee ID: 1001 Employee ID: 1002
In the above example, we have created two objects employee1 and employee2 of the Employee class.
Python Methods
We can also define a function inside a Python class. A Python Function defined inside a class is called a method.
# create a class class Room: length = 0.0 breadth = 0.0 # method to calculate area def calculate_area(self): print("Area of Room constructor">Python Constructors Earlier we assigned a default value to a class attribute,
class Bike: name = "" . # create object bike1 = Bike()
However, we can also initialize values using the constructors. For example,
class Bike: # constructor function def __init__(self, name = ""): self.name = name bike1 = Bike()
Here, __init__() is the constructor function that is called whenever a new object of that class is instantiated.
The constructor above initializes the value of the name attribute. We have used the self.name to refer to the name attribute of the bike1 object.
If we use a constructor to initialize values inside a class, we need to pass the corresponding value during the object creation of the class.
Here, "Mountain Bike" is passed to the name parameter of __init__() .
Table of Contents