A mix of previous answers :
from copy import deepcopy
def default(number, value = None):
if type(value) is dict or object:
return [deepcopy(value) for _ in range(number)]
else:
return [value] * number
o, p, q, r, s = default(5)
t, u = default(2, false)
v, w, x = default(3, {})
class GPU:
def __init__(self, m, p):
self.model = m
self.price = p
rtx_3080 = GPU("RTX 3080", 99999)
y, z = default(2, rtx_3080)
Edit:
Tried to optimize deepcopy call by better handling mutable variables with pandas/numpy types as well. Might have missed some other ones. If someone find a better way to check for mutability, feel free to share. Althought, this maybe over-engineering whatever your use case is...
import builtins
from copy import deepcopy
from numbers import Number
from pandas import api, DataFrame
mutables = (dict, list, set, DataFrame)
immutables = (str, tuple, frozenset, Number)
def is_built_in_type(var):
return type(var).__name__ in dir(builtins)
def is_mutable(var):
return var is not None and (api.types.is_list_like(var) or isinstance(var, mutables) or not is_built_in_type(var))
def is_immutable(var):
return var is None or isinstance(var, immutables)
def default(number, value=None):
if is_mutable(value):
return [deepcopy(value) for _ in range(number)]
elif is_immutable(value):
return [value] * number
else:
raise ValueError("Unexpected value type")
a, b, c, d, e = default(5)
f, g, h = default(3, False)
i, j = default(2, "False")
k, l, m, n = default(4, (3, 2, 1))
o, p = default(2, 3.14159265358979)
q, r, s = default(3, [1, 2, 3])
t, u = default(2, {})
v, w, x = default(3, DataFrame({'col1': [7, 13, 42, 73, 666], 'col2': [1, 0.6, 2, 1.4, 0.3]}))
class GPU:
def __init__(self, m, p):
self.model = m
self.price = p
rtx_3080 = GPU("RTX 3080", 99999)
y, z = default(2, rtx_3080)
dicitonary['details']
andKeyErrors
suck., plus ides won't highlight invalid keys but variables. If I have to usedetails = dicitonary['details']
, I'd better usedetails = None
than this round dict creations, lookups and KeyErrors. – Capitolinea,b=(True,)*2
Ora=b=True
– Penninite