Optional arguments in a class behave like class attributes.
Chris Angelico
rosuav at gmail.com
Mon Oct 17 10:57:13 EDT 2022
On Tue, 18 Oct 2022 at 01:39, Abderrahim Adrabi
<abderrahim.adrabi at gmail.com> wrote:
> So, these default values behave like class attributes, here is a demo:
>
> # Using a list -----------------------------
> class GameOne:
> def __init__(self, games = []) -> None:
> self.games = games
>
This makes the default be a single list, the same list for all of
them. If you want the default to be "construct a new list", you'll
need something else:
def __init__(self, games=None):
if games is None: games = []
There is an open proposal to allow this syntax, which would do what you want:
def __init__(self, games=>[]):
However, this is not part of any current version of Python.
ChrisA
More information about the Python-list
mailing list