Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutable default values shared by class instances #11

Closed
georgeto opened this issue Apr 8, 2023 · 3 comments
Closed

Mutable default values shared by class instances #11

georgeto opened this issue Apr 8, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@georgeto
Copy link

georgeto commented Apr 8, 2023

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
Copy link
Owner

sgraaf commented Apr 8, 2023

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 sgraaf self-assigned this Apr 8, 2023
@sgraaf sgraaf added the bug Something isn't working label Apr 8, 2023
@sgraaf sgraaf mentioned this issue Apr 8, 2023
@sgraaf
Copy link
Owner

sgraaf commented Apr 8, 2023

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 sgraaf closed this as completed in 0648a24 Apr 9, 2023
sgraaf added a commit that referenced this issue Apr 9, 2023
…bers, closes #10, closes #11

* ♻️ Reintroduce `__init__()` methods to all `Element`-inheriting classes

* 🐛 Fix serialization of route and track numbers

* 🔖 Bump version

* 📝 Update changelog
@sgraaf
Copy link
Owner

sgraaf commented Apr 9, 2023

This has been fixed with the 0.2.1 release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants