Python Classes and Objects
Python is an object oriented programming language.
In Python almost everything has its properties and methods as an object.
A Class or a "blueprint" to generating objects is like a constructor.
Create a Class
Use the term class
to build a class :
Example :- Create a class called MyClass that contains the property x :
class MyClass:
x = 5
print(MyClass)
Output :-
Create Object
Now we can make objects using the MyClass class:
Example :- Create the p1 name object and print the x value :
class MyClass:
x = 5
p1 = MyClass()
print(p1.x)
Output :-
The __init__() Function
The previous examples are the most simplistic classes and objects and are not very suitable for applications of real life.
We must comprehend the __init__()
built-in function for understanding the meaning of classes.
Every class has a function named __init__()
, which always is performed when you start the class.
Use the function __init__()
to assign object property values or other essential activities to do while creating the object :
Example :- Use the __init__()
function to allocate values for name and age to create a class called the person :
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
Output :-
36
Note : Whenever a class is used to create a new object, __init__()
is automatically invoked.
Object Methods
Objects may have methods as well. Object methods are functions that are part of the object.
Let us in Person Class build a method :
Example :- Insert a feature which prints a welcome and execute it on the object p1 :
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()
Output :-
Note : The Self
is a reference to the current class instance and is used to access class variables.
Related Links
The self Parameter
The self
parameter refers to the current class instance and is used to access class-related variables.
It doesn't need to be called self
, but it must be the primary parameter for any function in the class :
Example :- Instead of self use the terms mysillyobject or abc :
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()
Output :-
Modify Object Properties
You can change objects such like this :
Example :-
Set the age of p1 to 40 :
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.age = 40
print(p1.age)
Output :-
Delete Object Properties
The del
keyword allows you to remove property on objects :
Example :- Delete the object p1 from age property :
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)
del p1
print(p1)
Output :-
File "demo_class8.py", line 13, in < module>
print(p1)
NameError: 'p1' is not defined
Related Links
Delete Objects
By employing del
keyword, you can remove items.
Example :- Delete 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)
del p1
print(p1)
Output :-
File "demo_class8.py", line 13, in < module>
print(p1)
NameError: 'p1' is not defined
The pass Statement
class
definitions may not be empty, but if you have a class
definition without content for any reason, please use the pass
declaration to prevent an error.
Example :- class Person :
class Person:
pass
# having an empty class definition like this, would raise an error without the pass statement