0 votes
in Python by

Can you please clarify what is the yield of the accompanying Python Language code piece? Legitimize your answer. 

1 Answer

0 votes
by
def extendList(val, list=[]):

list.append(val)

bring list back

list1 = extendList(10)

list2 = extendList(123,[])

list3 = extendList('a')

print "list1 = %s" % list1

print "list2 = %s" % list2

print "list3 = %s" % list3

The consequence of the above Python Language code piece is:

list1 = [10, 'a']

list2 = [123]

list3 = [10, 'a']

You may wrongly expect list1 to be equivalent to [10] and list3 to coordinate with ['a'], believing that the rundown contention will instate to its default estimation of [] each time there is a call to the extendList.

Nonetheless, the stream resembles that another rundown gets made once after the capacity is characterized. What's more, the equivalent get utilized at whatever point somebody calls the extendList technique without a rundown contention. It works like this in light of the fact that the computation of articulations (in default contentions) happens at the hour of capacity definition, not during its summon.

The list1 and list3 are henceforth working on a similar default list, while list2 is running on a different item that it has made all alone (by passing an unfilled rundown as the estimation of the rundown boundary).

The meaning of the extendList capacity can get changed in the accompanying way.

def extendList(val, list=None):

on the off chance that rundown is None:

list = []

list.append(val)

bring list back

With this modified execution, the yield would be:

list1 = [10]

list2 = [123]

list3 = ['a']

Related questions

0 votes
asked Aug 30, 2020 in Python by sharadyadav1986
0 votes
asked Aug 30, 2020 in Python by sharadyadav1986
...