python - How to get the name of lists for comparison purposes -
this may sound bit funny, trying do: have 2 lists: mylist1
, mylist2
. passing these lists function perform specific task depending on list receives (i.e., based on name of list, not content).
def listprocessor(data): if data == 'mylist1': perform task1 else: #meaning data mylist2 perform task 2
is there way in python 3 inquire of name of list (or tuple, dictionary, etc.) , comparison this? obviously, way doing here isn't working since it's comparing content of list 'data' single string 'mylist1', isn't working! :-).
there few ways this:
you can create separate functions if needs done each list separate.
you can update function either:
take 2 lists argument:
def list_processor(list1=none, list2=none): if list1: # stuff list1 if list2: # stuff list2
you can add flag, identifying kind of action performed, , set default well:
def list_processor(some_list=none, type_of_list=1): if type_of_list == 1: # stuff some_list if list1 if type_of_list == 2: # stuff some_list if list2
you not want proposed, various reasons. 1 key reason in python, may call variables in other languages not "boxes put stuff in" (as textbooks refer them).
in python variables names point object. key thing multiple names can point same object; confuse function if rely on lists "name".
here example:
>>> = [1,2,3] >>> b = >>> b.append('hello') >>> b true >>> b == true >>> b [1, 2, 3, 'hello'] >>> [1, 2, 3, 'hello']
in example, both a
, b
pointing same list object. a
affects b
.
Comments
Post a Comment