Python – Objects and Classes

After reading this Python Objects and Classes topic, you will understand class, object, methods in object, changing object attributes, removing object attributes, and removing objects in Python.

A python is an object-oriented programming language in which everything considered as an object. An object having two parts as data and action. The data that object remembers and action that object can perform.

What is a Class in Python?

A class is a collection of objects of similar types.

How to create Class in python?

Use keyword class to create class in python.

For Example:

#class creating
class class_name:
# class attribute  
   a=15
   b=10

Explanation:

  • statement class class_name: describe that class created in python and class_name is the name of class.
  • In statements a=15 and b=10, a and b are the attributes of class.

How to create Object in python?

In the above topic we have already created class having name class_name and now we create objects, let us assume the name of objects y and z.

Example:

Program (1): To show how to create object in Python.

#class creating
class class_name:
# class attribute  
   a=15
   b=10
#create objects named as y and z
y=class_name()
z=class_name()
print(y.a)
print(z.b)

Output(1)

15
10

Explanation: for Program(1) and Output(1)

  • Here two objects named as y and z created for class_name and display the values of a and b.

Class built-in function __int()__

Whenever new object created for a class, the function __int()__ automatically called. The __int()__ function also used for assigning values to object attributes.

Example:

Program (1): To show how to use __int()__ function in Python.

#class creating
class student:
 def __init__(self, name, age):
    self.name = name
    self.age = age
#create object p1 and object attributes
p1 = student("shikhar", 28)
print(p1.name)
print(p1.age)

Output(1)

shikhar
28

Methods in Object

Methods are the functions, defined in the class. Methods in object are the function which deals with object only.

Example:

Program (1): To show how to use methods in object.

#creating class
class student:
  def __init__( self, name, age):
    self.name = name
    self.age = age
#method (function) in object to display student name
  def func_name(self):
    print("my name is " + self.name)
#create object p1 and object attributes
p1=student("shikhar",28)
p1.func_name()

Output(1)

my name is shikhar

Changing Object Attributes

We can change the attributes of object.

Example:

Program (1): To show how to change object attributes in Python.

#class creating
class student:
 def __init__(self, name, age):
    self.name = name
    self.age = age
#create object p1 and object attributes
p1 = student("shikhar", 28)
print(p1.name)
print(p1.age) 
#changing name and age i.e. change object attributes
p1.name = "virat"
p1.age = 30
print(p1.name)
print(p1.age)

Output(1)

shikhar
28
virat
30

Removing Objects Attributes

We can remove the attributes of objects by using del keyword.

Example:

Program (1): To show how to remove object attributes in Python.

#class creating
class student:
 def __init__(self, name, age):
    self.name = name
    self.age = age
#create object p1 and object attributes
p1 = student("shikhar", 28)
print(p1.name)
print(p1.age) 
#changing name and age i.e. change object attributes
p1.name = "virat"
p1.age = 30
print(p1.name)
print(p1.age)

Output(1)

shikhar
28
shikhar
Traceback (most recent call last):
File "C:/Users/rahul/Desktop/electrworkbook/pyton_web/add123.py", line 13, in <module>
print(p1.age)
AttributeError: 'student' object has no attribute 'age'

Explanation:

  • Here AttributeError: in output verify that attribute age gets removed.

Removing Objects

We can remove objects by using del keyword.

Example:

Program (1): To show how to remove objects in Python.

#creating class
class student:
 def __init__(self, name, age):
    self.name = name
    self.age = age
#create object p1 and object attributes
p1 = student("shikhar", 28)
print(p1.name)
print(p1.age) 
#remove age i.e. remove object attribute
del p1.age
print(p1.name)
print(p1.age)

Output(1)

shikhar
28
Traceback (most recent call last):
File "C:/Users/rahul/Desktop/electrworkbook/pyton_web/add123.py", line 12, in <module>
print(p1.name)
NameError: name 'p1' is not defined

Explanation:

  • Here NameError: in output verify that object p1 gets removed.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

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