WHAT IS A CLASS AND OBJECT IN PYTHON


Definition of Python Class and Object

Python Class

The Class is a logical entity that has some attributes and methods, in other words, we can also say that the class is like an object constructor or a “blueprint” for creating objects.

Python Objects

The Object is an entity that has a state and behavior.

How to Create a Class?

Create Class

We can create a class by using the keyword class.

An example of the class is provided below showing the creation of a class named MyClass with an attribute named studentsno.

Python Code

class MyClass:
  studentsno = 50
print(MyClass)

Output

<class '__main__.MyClass'>
How to Create Class Object in Python Class?

The object can be created by using the class name as shown in the below example

class MyClass:
  studentsno = 50

Obj1=MyClass()
print(Obj1.studentsno)

Output

50
What is The _init_() Function in Python?

The __init__() function is executed when the class is initiated. It is used to assign values to object properties, or other operations that are necessary to do when the object is being created.

Python Code

class Person:
  def __init__(self, Fname, Lname):
    self.Fname = Fname
    self.Lname = Lname

p1 = Person("John", "Doe")

print(p1.Fname)
print(p1.Lname)

Output

John
Doe
What is The __str__() Function in Python Classes?

_str_() method

The _str_() method is used to set the return value when the class object is represented as a string.

In case we are not setting the _str_() method the string representation of the object is returned as shown below-

Python Code

class Person:
  def __init__(self, Fname, Lname):
    self.Fname = Fname
    self.Lname = Lname

p1 = Person("John", "Doe")

print(p1)

Output

<__main__.Person object at 0x2b20eb127100>

In case we are setting the _str_() method the string representation of the object is returned as shown below-

Python Code

class Person:
  def __init__(self, Fname, Lname):
    self.Fname = Fname
    self.Lname = Lname
  def __str__(self):
    return f"{self.Fname} {self.Lname}" 

p1 = Person("John", "Doe")

print(p1)

Output

John Doe
What are the Object Methods in Python?

Object Methods

Methods are used to provide the functionality to the class objects, they are the functions defined inside the class and belong to the object.

In the below example, we have created a class Person and defined a method named myfunction inside the class Person and executed it on the P1 object which will print the name provided as attributes at the time of the creation of P1 (class object).

Python Code

class Person:
  def __init__(self, Fname, Lname):
    self.Fname = Fname
    self.Lname = Lname
  def myfunction(self):
     print(self.Fname, self.Lname)
p1 = Person("John", "Doe")

p1.myfunction()

Output

John Doe
What is a self Parameter in Python?

The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class. Its use is shown in the above examples.

It does not have to be named self, we can call it whatever we like, but it has to be the first parameter of any function in the class, as shown in the below example we are using the parameter abc.

Python code to show the use of self parameter

class Person:
  def __init__(abc, Fname, Lname):
    abc.Fname = Fname
    abc.Lname = Lname
  def myfunction(abc):
     print(abc.Fname, abc.Lname)
p1 = Person("John", "Doe")

p1.myfunction()

Output

John Doe
How to Modify Object Properties in Python?

Modify Object Properties

We can modify the object properties as shown in below example in which we are setting the last name Lname parameter of object P1(refer to above example) to Reufes

Python Code

P1.Lname=Reufes
P1.myfunction()

Output

John Reufes
How to del object properties?

Delete Object Properties

We can delete properties of objects by using the del keyword

In the below example by using del keyword we are deleting the Lname property from P1 Object used in the above example

Python Code

class Person:
  def __init__(self, Fname, Lname):
    self.Fname = Fname
    self.Lname = Lname
  def myfunction(self):
     print(self.Fname, self.Lname)
p1 = Person("John", "Doe")


del p1.Lname
print(p1.Lname)

Output

AttributeError: 'Person' object has no attribute 'Lname'
How to Delete Objects in Python?

Delete Objects

We can delete objects by using the del keyword:

Example
Delete the p1 object:

Python Code

class Person:
  def __init__(self, Fname, Lname):
    self.Fname = Fname
    self.Lname = Lname
  def myfunction(self):
     print(self.Fname, self.Lname)
p1 = Person("John", "Doe")


del p1
print(p1)

Output

NameError: name 'p1' is not defined
What is the use of Pass Statement in python class?

The pass Statement

Class definitions cannot be empty.

In case we want a class definition without content, we need to put in the pass statement to avoid getting an error.

Python Code

class Person:
  pass #This class wont give an error
Built-in class functions in python
SNFunctionDescription
1getattr(obj,name,default)It is used to access the attribute of the object.
2setattr(obj, name,value)It is used to set a particular value to the specific attribute of an object.
3delattr(obj, name)It is used to delete a specific attribute.
4hasattr(obj, name)It returns true if the object contains some specific attribute.
List of Built-in Class Functions in Python
Built-in class attributes in Python
SNAttributeDescription
1__dict__It provides the dictionary containing the information about the class namespace.
2__doc__It contains a string which has the class documentation
3__name__It is used to access the class name.
4__module__It is used to access the module in which, this class is defined.
5__bases__It contains a tuple including all base classes.
List of Built-in Class Attributes in Python

Leave a Reply

Your email address will not be published. Required fields are marked *