Python function with empty list parameter

Viewed 30

I came across this function where an empty list is passed in as a parameter in the function.

def myFunction(value, values=[]):
    values.append(value)
    return values

myFunction(1)
myFunction(2)
myFunction(3)

The output is:

[1,2,3]

instead of:

[3]

Does this only work for lists? When I tried doing it with just an integer, it does not work.

def myNumber(value, values = 10):
    if value > 100:
        values += 2
        return values
        
myNumber(101)
myNumber(102)
0 Answers
Related