Я писал много таких конструкций за последние пару дней:
list = get_list()
if list:
for i in list:
pass # do something with the list
else:
pass # do something if the list was empty
Много мусора, и я назначаю список реальной переменной (сохраняя ее в памяти дольше, чем нужно). Python упростил мой код до сих пор... Есть ли простой способ сделать это?
(Я понимаю, что else
в конструкторе for: else:
всегда запускается после того, как он зациклился, пуст или нет - так что не хочу)