About 451,000 results
Open links in new tab
  1. Default value in a function in Python - Stack Overflow

    Apr 11, 2016 · Rule 1. Do not use mutable objects as default values. def anyFunction( arg=[] ): Will not create a fresh list object. The default list object for arg will be shared all over the place. …

  2. Best practice for setting the default value of a parameter that's ...

    Mar 2, 2012 · The problem with a mutable default argument is that it will be shared between all invocations of the function -- see the "important warning" in the relevant section of the Python …

  3. python - How to set default value for variable? - Stack Overflow

    Jul 13, 2022 · In the exact scenario you present, you can use default values for arguments, as other answers show. Generically, you can use the or keyword in Python pretty similarly to the …

  4. How should I declare default values for instance variables in Python?

    With dataclasses, a feature added in Python 3.7, there is now yet another (quite convenient) way to achieve setting default values on class instances. The decorator dataclass will automatically …

  5. python - Get a function argument's default value? - Stack Overflow

    Since in Python arguments with default values must always be the last ones, using -on len of __defaults__ (with the : to get until the end of the array) will bring up the argument names …

  6. python - How do I add default parameters to functions when …

    The empty dict passed as a default parameter is the same dict for every call. So if the function mutates it then the default next time will be the mutated value from last time. Making None the …

  7. Is there shorthand for returning a default value if None in Python ...

    It is a frequent source of errors that are hard to debug, because for many subtle reasons x could evaluate to false even if it is the desired value. – Quant Metropolis Commented Jun 23, 2016 …

  8. How to define default value if empty user input in Python?

    Mar 19, 2019 · Here I have to set the default value if the user will enter the value from the keyboard. Here is the code that user can enter value: input = int(raw_input("Enter the inputs : …

  9. Python function with default list argument - Stack Overflow

    Python stores default values for optional arguments. If you modify their value, it will still be modified in the subsequent calls. There's a good explanation on this thread: Python optional …

  10. python - Calling a function with default value for arguments

    Feb 21, 2019 · In this case, inside the A() function, B() will be called taking the default values for the last two: B(argB1= K*argA1, argB2= K* defValA2, argB3= K* defValA3) However, I’d like …

Refresh