xxxxxxxxxx
# Python program to illustrate destructor
class A:
def __init__(self, bb):
self.b = bb
class B:
def __init__(self):
self.a = A(self)
def __del__(self):
print("die")
def fun():
b = B()
fun()
xxxxxxxxxx
x, y = (5, 11)
people = [
("James", 42, "M"),
("Bob", 24, "M"),
("Ana", 32, "F")
]
for name, age, gender in people:
print(f"Name: {name}, Age: {age}, Profession: {gender}")
# use _ to ignore a value
person = ("Bob", 42, "Mechanic")
name, _, profession = person
head, *tail = [1, 2, 3, 4, 5]
print(head) # 1
print(tail) # [2, 3, 4, 5]
*head, tail = [1, 2, 3, 4, 5]
print(head) # [1, 2, 3, 4]
print(tail) # 5
head, *middle, tail = [1, 2, 3, 4, 5]
print(head) # 1
print(middle) # [2, 3, 4]
print(tail) # 5
head, *_, tail = [1, 2, 3, 4, 5]
print(head, tail) # 1 5
from operator import itemgetter
params = {'a': 1, 'b': 2, 'c': 3}
# return keys a and c
a, c = itemgetter('a', 'c')(params)
print(a, c)
# output 1 3
xxxxxxxxxx
# Python program to illustrate destructor
class Employee:
# Initializing
def __init__(self):
print('Employee created')
# Calling destructor
def __del__(self):
print("Destructor called")
def Create_obj():
print('Making Object...')
obj = Employee()
print('function end...')
return obj
print('Calling Create_obj() function...')
obj = Create_obj()
print('Program End...')
xxxxxxxxxx
# Python program to illustrate destructor
class Employee:
# Initializing
def __init__(self):
print('Employee created.')
# Deleting (Calling destructor)
def __del__(self):
print('Destructor called, Employee deleted.')
obj = Employee()
del obj
xxxxxxxxxx
class MyClass:
def __init__(self):
# Constructor
def __del__(self):
# Destructor
# Code block for clean-up actions
# Instantiate the class
obj = MyClass()
# Perform operations with the object...
# The destructor will automatically be called when the object goes out of scope