Jovian
⭐️
Sign In

Python Classes/Objects

Creating a Class

To create a class, use the keyword class:

In [8]:

class Student: # Base class/super class for all students
   Count = 0 # student count

   def __init__(self, name, rollno):
      self.name = name
      self.rollno = rollno
      Student.Count += 1
   
   def ShowCount(self):
     print("Total Students: %d" % Student.Count)

   def ShowStudent(self):
      print("Name : ", self.name,  ", Roll_no: ", self.rollno)

A class named Student, with different methods

Creating an Object

In [10]:
std1 = Student("Zara", 12)
std2 = Student("Manu", 50)
std1.ShowStudent()
std2.ShowStudent()
print("Total number of Students: %d" % Student.Count)
Name : Zara , Roll_no: 12 Name : Manu , Roll_no: 50 Total number of Students: 5

Created objects named std1,std2 , displayed their name and roll_no and print the total number of students:

The init() Function : Python Constructors

A constructor is a class function that instantiates an object to predefined values.

Let's see an example..

In [6]:
class Person:
  def __init__(self, name, age,place):
    self.name = name
    self.age = age
    self.place = place

obj = Person("John", 36,"London")

print(obj.name)
print(obj.age)
print(obj.place)
John 36 London

Created a class named Person, use the init() function to assign values for name , age and place:

Note: The init() function is called automatically every time the class is being used to create a new object.

Object Methods

Objects can also contain methods. Methods in objects are functions that belong to the object.

In [13]:
class Person:
  def __init__(self, name, age, place):
    self.name = name
    self.age = age
    self.place = place
    
  def myfunc(self):
    print("Hello my name is " + self.name)
    print("I am " + str(self.age) + " years old")
    print("and i'm from "+ self.place)

obj = Person("John", 36 , "London")
obj.myfunc()
Hello my name is John I am 36 years old and i'm from London

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:

In [21]:
class person():
    def __init__(myobj1,name,age,place):
        myobj1.name = name
        myobj1.age = age
        myobj1.place = place
        
    def myfun(myobj2):
        print("My name is " + myobj2.name)
        print("im " + str(myobj2.age) + " and im from " + myobj2.place)
        
obj = person("Tom",25,"California")
obj.myfun()
My name is Tom im 25 and im from California

Used the words myobj1 and myobj2 instead of self:

Modifying Object Properties

In [27]:
class person():
    def __init__(myobj1,name,age,place):
        myobj1.name = name
        myobj1.age = age
        myobj1.place = place
        
    def myfun(myobj2):
        print("My name is " + myobj2.name + " , im " + str(myobj2.age) + " and im from " + myobj2.place)
        
obj = person("Tom",25,"California")
obj.myfun()

obj.name = "Jerry"
obj.age = 30
obj.place = "London"
print(obj.name,obj.age,obj.place)
My name is Tom , im 25 and im from California Jerry 30 London

Deleting Object Properties

You can delete properties on objects by using the del keyword:

In [26]:
del obj.age

Deleted the age property from the obj object:

Deleting Objects

You can delete objects by using the del keyword:

In [29]:
del obj

The pass Statement

class definitions cannot be empty, but if for some reason you have a class definition with no content, put in the pass statement to avoid getting an error.

In [32]:
class Person:
  pass