Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python destructuring

head, *tail = [1, 2, 3, 4, 5]

print(head)  # 1
print(tail)  # [2, 3, 4, 5]
Comment

destructor module in python

def __del__(self):
  # body of destructor
Comment

python destructor

# 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
Comment

python destructor

# 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()
Comment

python destructuring

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
Comment

PREVIOUS NEXT
Code Example
Python :: Read multiple csv files into separate dataframes Python 
Python :: import turtle in python 
Python :: python function to multiply two numbers 
Python :: create set in python 
Python :: convert dictionary to string 
Python :: python formatting string 
Python :: typing python 
Python :: python add column with constant value 
Python :: what are tuples in python 
Python :: python set union 
Python :: for range python 
Python :: what is readline() in python 
Python :: py scrapy 
Python :: gfg placement 
Python :: numpy and operator 
Python :: math floor python 
Python :: python value error 
Python :: how to make python print 2 line text in one code 
Python :: set empty dictionary key python 
Python :: add dataframe column to set 
Python :: how to print name in python 
Python :: python __new__ 
Python :: what is a thread in os 
Python :: how to update image in django 
Python :: how to make a calculator in python 
Python :: python boto3 put_object to s3 
Python :: use chrome console in selenium 
Python :: sns histplot change legend labels 
Python :: selenium check if driver is open python 
Python :: negate an int in python 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =