Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python destructuring

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

print(head)  # 1
print(tail)  # [2, 3, 4, 5]
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

array destructuring python

example_list = ["A", "B", "C"]

for counter, letter in enumerate(example_list):
	print(counter, letter)

# 0 A
# 1 B
# 2 C
Comment

PREVIOUS NEXT
Code Example
Python :: search and replace in python 
Python :: How to Add a overall Title to Seaborn Plots 
Python :: how to code a funtion in python 
Python :: py array contains 
Python :: plot dataframe 
Python :: python regeression line 
Python :: github3 python 
Python :: server in python 
Python :: python calendar table view 
Python :: how to convert str to int python 
Python :: recurrent neural network pytorch 
Python :: lambda expression python 
Python :: how to find greatest number in python 
Python :: order_by django queryset order by ordering 
Python :: discord py server.channels 
Python :: python web scraping 
Python :: euclidean distance 
Python :: Removing Elements from Python Dictionary Using popitem() method 
Python :: string manipulation in python 
Python :: time conversion 
Python :: class method in python 
Python :: shape of variable python 
Python :: pairs with specific difference 
Python :: set intersection 
Python :: convert date to string in python 
Python :: how to use djoser signals 
Python :: python functools 
Python :: data encapsulation in python 
Python :: Ignoring invalid distribution -ip (c:python310libsite-packages) 
Python :: generator expression 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =