Jovian
⭐️
Sign In

Python Classes/Objects

Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is a logical grouping of data and functions. It gives the freedom to create data structures that contains arbitrary content and hence easily accessible.

Creating a Class

To create a class, use the keyword class:

In [2]:
class MyClass:
  x = 5

A class named MyClass, with a property named x:

Creating an Object

In [3]:
obj = MyClass()
print(obj.x)
5

Created an object named obj, and print the value of x:

The init() Function : Python Constructors

A constructor is a class function that instantiates an object to predefined values. It begins with a double underscore (_),__init__() function. 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:

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 [31]:
class Person:
  pass
In [ ]:
 
In [ ]:
 
In [ ]: