Reputation: 1145
I have two filenames as input arguments. Both filenames go through the same process, and their default values are None
. I write my function like this:
import logging
def my_func(filename_list=[None]*2):
for i in filename_list:
if i is None:
logging.info('One filename is None')
else:
# do stuff
Is there a better way to do it in python instead of using [None]*2
?
Upvotes: 0
Views: 80
Reputation: 532418
Just use None
as the default value. You can create a list of None
inside the file.
def my_func(filename_list=None):
if filename_list is None:
filename_list = [None, None]
for i in filename_list:
...
What is special about a list of 2 Nones
, though? If my_func
always takes a list of 2 filenames, I would recommend just passing them explicitly:
def my_func(file1=None, file2=None):
for i in [file1, file2]:
...
Otherwise, an empty list would seem reasonable:
def my_func(filename_list=None):
if file_name_list is None:
filename_list = []
for i in filename_list:
...
Upvotes: 1
Reputation: 833
The most pythonic way is:
import logging
def my_func(first_filename, second_filename):
file_names = [first_filename, second_filename]
for i in filename_list:
if i is None:
logging.info('One filename is None')
else:
# do stuff
If you know you have 2 files for sure each call, do not pass a list as argument. There is no point.
Upvotes: 1