In Python, why do function parameters maintain their value beetween function calls

Mar Ok

I'm fairly new at Python and i cannot wrap my head around the results im getting Using the code below:

def func(a,b=set()):
    res=list()
    for i in a:
        if i not in b:
            res.append(i)
            b|={i}
    return res

print(func([1,1,2,2,3,4]))
print(func([1,1,2,2,3,4]))

I was getting output:

[1,2,3,4]
[]

I put "print(b)" above "res=list()" and got output:

set()
[1,2,3,4]
{1,2,3,4}
[]

What is going on? Shouldn't "b" be set to "set()" when i call the function? Im using Python 3.6

stybl

Have a look at the documentation for default parameters:

The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes.

When you define a function with a default parameter, the default value is only evaluated when the definition is first executed by the interpreter (the actual def statement). This is usually not a problem, except for when a mutable default value is used. That is to say, one that can be modified in place.

In your case, when you modify b in your function the first time you call it, it keeps that value the next time around. To avoid this, you can do like so:

def func(a,b=None):
    if b is None:
        b = set()
    res=list()
    for i in a:
        if i not in b:
            res.append(i)
            b|={i}
    return res

Now b will always have the default value you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pattern match parameters to function calls Python?

From Dev

Extracting function calls with parameters

From Dev

Why both the function calls return the same value?

From Dev

Maintain generator object across function calls

From Dev

Separating Parameters in Repeated Function Calls

From Dev

Pass function and parameters as parameters to intermediary function which then calls named function

From Dev

Why is this value `save`d between calls in this Fortran function?

From Dev

Why do C++ STL function calls need to be so verbose?

From Dev

Why do argument-less function calls execute faster?

From Dev

Why do mutable default arguments remember mutations between function calls?

From Dev

Why do some function calls fail to work without a type application?

From Dev

python recursive function calls

From Dev

How do I pass constant parameters through an optimization function that calls another function in R?

From Java

Why does Python allow function calls with wrong number of arguments?

From Dev

Python: Why 2 calls of the same function don't run in parallel?

From Dev

MODERN OPENGL can't change uniform value beetween glDrawArrays calls

From Dev

Why is const unnecessary in function declarations in header files for parameters passed by value?

From Dev

Why can't I assign lambdas to function parameters as a default value?

From Dev

Why would this function wont receive my inputbox.value as parameters?

From Dev

Python: How do I wrap function calls, not the functions themselves?

From Dev

Identically named parameters for nested function calls and ellipsis

From Dev

How function calls with parameters work in C?

From Dev

Why is function returning none instead of the value? (Python)

From Dev

Python function passing parameters

From Dev

Check parameters of a function in a Python

From Dev

passing parameters in python function

From Dev

Why do we pass a function as a parameter in python

From Dev

intercept function calls going out of a function python

From Dev

Python - Mocking chained function calls

Related Related

  1. 1

    Pattern match parameters to function calls Python?

  2. 2

    Extracting function calls with parameters

  3. 3

    Why both the function calls return the same value?

  4. 4

    Maintain generator object across function calls

  5. 5

    Separating Parameters in Repeated Function Calls

  6. 6

    Pass function and parameters as parameters to intermediary function which then calls named function

  7. 7

    Why is this value `save`d between calls in this Fortran function?

  8. 8

    Why do C++ STL function calls need to be so verbose?

  9. 9

    Why do argument-less function calls execute faster?

  10. 10

    Why do mutable default arguments remember mutations between function calls?

  11. 11

    Why do some function calls fail to work without a type application?

  12. 12

    python recursive function calls

  13. 13

    How do I pass constant parameters through an optimization function that calls another function in R?

  14. 14

    Why does Python allow function calls with wrong number of arguments?

  15. 15

    Python: Why 2 calls of the same function don't run in parallel?

  16. 16

    MODERN OPENGL can't change uniform value beetween glDrawArrays calls

  17. 17

    Why is const unnecessary in function declarations in header files for parameters passed by value?

  18. 18

    Why can't I assign lambdas to function parameters as a default value?

  19. 19

    Why would this function wont receive my inputbox.value as parameters?

  20. 20

    Python: How do I wrap function calls, not the functions themselves?

  21. 21

    Identically named parameters for nested function calls and ellipsis

  22. 22

    How function calls with parameters work in C?

  23. 23

    Why is function returning none instead of the value? (Python)

  24. 24

    Python function passing parameters

  25. 25

    Check parameters of a function in a Python

  26. 26

    passing parameters in python function

  27. 27

    Why do we pass a function as a parameter in python

  28. 28

    intercept function calls going out of a function python

  29. 29

    Python - Mocking chained function calls

HotTag

Archive