class student_kit():
def register(self,name):# method declaration
self.name=name
print("Welcome:",self.name)
s1=student_kit() # object declaration
s1.register('Alex')
del s1
s1.register('Ron') # NameError
The last line in above code will generate error. We can use try except error handling to manage the error.
class student_kit():
def register(self,name):# method declaration
self.name=name
print("Welcome:",self.name)
try:
s1=student_kit() # object declaration
s1.register('Alex')
del s1
s1.register('Ron') # NameError
except NameError as my_msg:
print ("This is a NameError")
print(my_msg)
except:
print ("Some other error has occurred")
Output
Welcome: Alex
This is a NameError
name 's1' is not defined
Her we have deleted the object of a user defined class. In python everything is an object so del can delete list, dictionary, variable etc.
my_list=['a','b','c','d']
del my_list[1]
print(my_list)
Output
['a', 'c', 'd']
Deleting the list.
my_list=['a','b','c','d']
del my_list
print(my_list) # NameError
The last line will generate NameError.
my_dict={1:'Alex',2:'Ronald'}
print(my_dict)
del my_dict[2]
print(my_dict)
Output is here
{1: 'Alex', 2: 'Ronald'}
{1: 'Alex'}
We can delete the dictionary, here the change in line 3 is only displayed. This will generate error.
del my_dict
a=6
print(a) # 6
del a
print(a) # error
The last line will generate error. We can use try except error handling to manage the error.
a=6
try:
print(a) # 6
del a
print(a) # error
except NameError as my_msg:
print ("This is a NameError")
print(my_msg)
except:
print ("Some other error has occured")
Output
6
This is a NameError
name 'a' is not defined
Reserved Keywords in Python
variables in Python
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.