Python Forum
What data types can I use for default values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What data types can I use for default values?
#1
I'm working my way through _Teach Yourself Visually Python_ (2022). This example was presented:
def parlay(odds1, odds2, odds3 = None, odds4 = None, odds5 = None):
...
Following the example, a Tip is given about what data types to use for default values: "You can use most data types, including None, as in this example... However, in general, it is best to avoid mutable data types because although they work correctly the first time you call the function, subsequent calls to the function will return the last call assigned to the data type. For example, if you use an empty list as a default value, the first call returns an empty list, as expected, but the next call returns a list containing the values you assigned to the list."

I don't follow. Can someone give an example to illustrate this point and why using a mutable data type as default value might cause problems?

Thank you!
Reply
#2
https://docs.python-guide.org/writing/go...-arguments
https://stackoverflow.com/q/1132941/4046632
def spam(start=[]):
    start.append(1)
    print(start)


spam()
spam()
Probably you would expect that each call starts with empty dict and get

Output:
[1] [1]
but what you get is
Output:
[1] [1, 1]
The reason - default arguments are evaluated once - at function definition and because you mutate the list when calling the function, the result (i.e. modified list) from previous calls is preserved and that affects next calls.

To prevent this

def spam(start=None):
    if start is None:
        start=[]
    start.append(1)
    print(start)


spam()
spam()
Output:
[1] [1]
There are specific cases where this behaviour might be desirable and if so it can be used, i.e. there is no problem using mutable default arguments as long as you know what to expect.
Mark17 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  In SQL Server, mix data types. shiv11 0 889 Sep-21-2022, 12:50 PM
Last Post: shiv11
  I need to add data types to cython conversion python to c Good_AI_User 1 1,018 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  How to get evenly-spaced datetime tick labels regardless of x-values of data points? Mark17 4 5,261 Apr-04-2022, 07:10 PM
Last Post: Mark17
  How to insert different types of data into a function DrData82 0 1,259 Feb-10-2022, 10:41 PM
Last Post: DrData82
  Default values of arguments with the same id DrVictor 3 2,289 Dec-02-2020, 03:27 PM
Last Post: deanhystad
  List of mixed data types to bytes medatib531 1 2,349 Mar-16-2020, 11:53 AM
Last Post: scidam
  Function with many arguments, with some default values medatib531 3 2,610 Mar-14-2020, 02:39 AM
Last Post: medatib531
  Filtering Excel Document Data Based On Numerical Values eddywinch82 30 10,799 Feb-25-2020, 06:08 PM
Last Post: eddywinch82
  Changing Data Types BallisticSwami 2 2,425 Jun-27-2019, 01:17 PM
Last Post: BallisticSwami
  Type error when reading in different data types on an __init__ method Dylanmull 3 2,812 May-09-2019, 02:05 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020