[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gnulib-tool.py: Don't use mutable default arguments.
From: |
Bruno Haible |
Subject: |
Re: gnulib-tool.py: Don't use mutable default arguments. |
Date: |
Sun, 14 Apr 2024 08:13:43 +0200 |
Hi Collin,
> Here is a test program to demonstrate:
>
> --------------------------------
> #!/usr/bin/env python3
>
> def function(arg1, arg2 = dict()):
> arg2[arg1] = 0
> print(arg2)
>
> function('one')
> function('two')
> function('three')
> --------------------------------
>
> When executing the following is printed:
>
> {'one': 0}
> {'one': 0, 'two': 0}
> {'one': 0, 'two': 0, 'three': 0}
Oh, this is surprising. I would have expected that the default value
expression gets evaluated each time, not once only.
Thanks for the observation! Patch applied.
Bruno