WHAT IS A CLASS AND OBJECT IN PYTHON?

663 viewsPYTHONPython
0

WHAT IS A CLASS AND OBJECT IN PYTHON?

Saraswat World Changed status to publish June 16, 2023
0
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
SN Function Description
1 getattr(obj,name,default) It is used to access the attribute of the object.
2 setattr(obj, name,value) It is used to set a particular value to the specific attribute of an object.
3 delattr(obj, name) It is used to delete a specific attribute.
4 hasattr(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
SN Attribute Description
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
Saraswat World Changed status to publish June 16, 2023
Write your answer.

Posted

in

Tags:

Comments

Leave a Reply

A Tribute to Dilip Kumar