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

Eliminate mutable default parameters from methods #210

Conversation

kurtmckee
Copy link
Contributor

Fixed

  • Eliminate mutable default parameters from methods.

    Because the parameters are passed to observers, it is possible for an observer to accidentally -- and permanently -- modify a default argument.

    observers = [lambda x: x.append("bad")]
    
    def demo(groups=[]):
        print(groups)
        for observer in observers:
            observer(groups)
    
    demo()  # []
    demo()  # ["bad"]
    demo()  # ["bad", "bad"]

Fixed
-----

* Eliminate mutable default parameters from methods.

  Because the parameters are passed to observers,
  it is possible for an observer to accidentally
  -- and permanently -- modify a default argument.

  ```python
  observers = [lambda x: x.append("bad")]

  def demo(groups=[]):
      print(groups)
      for observer in observers:
          observer(groups)

  demo()  # []
  demo()  # ["bad"]
  demo()  # ["bad", "bad"]
  ```
@coveralls
Copy link

coveralls commented Oct 14, 2024

Coverage Status

coverage: 64.748% (-0.2%) from 64.919%
when pulling 0224bcc on kurtmckee:rm-mutable-parameter-defaults
into 0186f21 on LudovicRousseau:master.

@LudovicRousseau LudovicRousseau merged commit 3eb3feb into LudovicRousseau:master Oct 14, 2024
24 of 25 checks passed
@LudovicRousseau
Copy link
Owner

Thanks

@kurtmckee kurtmckee deleted the rm-mutable-parameter-defaults branch October 14, 2024 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants