a = 1234
b = 1234
print(id(a))
print(id(b))
print(a is b)
# 2221342169168
# 2221342169168
# True
>>> a = 1234
>>> id(a)
2715578112304
>>> b = 1234
>>> id(b)
2715578112432
>>>
>>> a is b
False
def proc1(b: str) -> str:
return 'A' + b
def proc2(b: str) -> str:
return f'A{b}'
dis.dis(proc1)
# 2 0 LOAD_CONST 1 ('A')
# 2 LOAD_FAST 0 (b)
# 4 BINARY_ADD
# 6 RETURN_VALUE
dis.dis(proc2)
# 5 0 LOAD_CONST 1 ('A')
# 2 LOAD_FAST 0 (b)
# 4 FORMAT_VALUE 0
# 6 BUILD_STRING 2
# 8 RETURN_VALUE
%timeit proc1('b')
# 72.7 ns ± 0.688 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
%timeit proc2('b')
# 74.6 ns ± 0.689 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
posl = ['Один', 'Два', 'Три', ['Четыре', 'Пять', 'Шесть', ['Семь', 'Восемь', ['Девять']] ], 'Десять']
def unpack_list(somelist):
res = []
def to_flat(item):
if isinstance(item, list):
for im in item:
to_flat(im)
else:
res.append(item)
return to_flat(somelist)
print(unpack_list(posl))
# ['Один', 'Два', 'Три', 'Четыре', 'Пять', 'Шесть', 'Семь', 'Восемь', 'Девять', 'Десять']