0% found this document useful (0 votes)
10 views6 pages

Python-3-2

__del__ is a magic method in Python that acts as a destructor, called when an object's references are deleted and it is garbage collected. The document also discusses the __repr__ method, which returns a string representation of an object, and lists built-in class attributes that provide information about the class. Additionally, it suggests writing programs that utilize classes for various purposes, such as storing student marks and employee details.

Uploaded by

23b01a1253
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
10 views6 pages

Python-3-2

__del__ is a magic method in Python that acts as a destructor, called when an object's references are deleted and it is garbage collected. The document also discusses the __repr__ method, which returns a string representation of an object, and lists built-in class attributes that provide information about the class. Additionally, it suggests writing programs that utilize classes for various purposes, such as storing student marks and employee details.

Uploaded by

23b01a1253
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 6

__del__ is dunder or magic method in Python.

Dunder or magic method in Python is the method


having two prefix and suffix underscores in the
method name.
Dunder here means “Double Under (Underscores)”.
These are commonly used for operator
overloading.
__del__
__del__ is a destructor method which is called as
soon as all references of the object are deleted i.e
when an object is garbage collected.
# Python program to demonstrate
# __del__
Syntax: class Example:
def # Initializing
def __init__(self):

__del__(self): print("Example Instance.")

body of # Calling destructor


def __del__(self):

destructor
print("Destructor called, Example
deleted.")

. obj = Example()
del obj
.
Note : The destructor was called after the program ended or when all the
references to object are deleted i.e when the reference count becomes zero,
not when object went out of scope.
pr__()
on __repr__() function returns the object representation in string format.

method is called when repr() function is invoked on the object.


ssible, the string returned should be a valid Python expression that can be used to reconstruct the object a
# A simple Person class

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __repr__(self):
rep = 'Person(' + self.name + ',' + str(self.age) +
')'
return rep

# Let's make a Person object and print the results of


repr()

person = Person("John", 20)


print(repr(person))
The built-in class attributes provide us with information about the
class.
Using the dot (.) operator, we may access the built-in class attributes.
The built-in class attributes in python are listed below −
Attributes Description
__dict__ Dictionary containing the class namespace

__doc__ If there is a class documentation class, this


returns it. Otherwise, None

__name__ Class name.

__module__ Module name in which the class is defined.


This attribute is "__main__" in interactive
mode.

__bases__ A possibly empty tuple containing the base


classes, in the order of their occurrence in
the base class list.
• Write a program that uses class to store the name and marks of
student
• Write a program with class Employee that keeps track of the number
of employees in an organization and also store their name,
designation and salary details.
• Write a program that has a class Circle, use a class variable to define
the value of constant PI. Use this class variable to calculate area and
circumference of a circle with specified radius.

You might also like