В том-то и дело, что вы не можете передать None
в with
, поскольку __exit__
вызывается, когда with
закончен. Вместо этого измените это на:
def call_method(o1, o2, f_in):
# If nothing special is done with None (just a pass) then you don't
# even need to call this with None, otherwise, you can check f_in like:
if f_in is None:
pass
else:
pass
if flag:
with open(path, 'rb') as f_in:
call_method(opt1, opt2, f_in)
else:
# only really needed if call_method does something if None is passed
call_method(opt1, opt2, None)
Другой вариант - использовать nullcontext()
вместо None
, как указано в другом ответе