sgraaf/gpx

Mutable default values shared by class instances

georgeto opened this issue ยท 3 comments

Some classes have fields with mutable value (e.g. lists), which are assigned a default value (e.g. empty list). If you have several instances of such a class, they each share the default values.

See the following example for illustration:

my_track_seg = TrackSegement()

my_track_a = Track()
my_track_b = Track()

# Add track segment to track A.
my_track_a.trksegs.append(my_track_seg)

# Because all instances of Track share the same mutable default value for `trksegs`,
# the previous statement adds the track segment also to track B.
assert my_track_a.trksegs[0] == my_track_seg
assert my_track_b.trksegs[0] == my_track_seg

I assume this was an oversight and not a deliberate design decision. For a detailed explanation and possible solutions, see this section in the Python documentation.

sgraaf commented

Hi @georgeto, thank you for contributing! ๐Ÿ˜„

And here I thought I was being clever by making use of mixin classes and fields instead of initializing these attributes in an __init__() method... This is indeed an oversight, and not a deliberate design decision... In fact: this is a regression, as this wasn't a problem in version 0.1.1!

Anyhow, thank you for spotting and reporting this (and providing me with resources on possible solutions)! I will implement and release a fix soon with version 0.2.1 (along with your PR #10).

sgraaf commented

Also: this is example # 9000 in the case for adding a test suite to the project. I have just opened a new issue (#12) for this. Feel free to add to the discussion there if you have additional thoughts and opinions.

sgraaf commented

This has been fixed with the 0.2.1 release!