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

Use ChainMap to handle overriding default kwargs #622

Open
TomNicholas opened this issue Feb 28, 2024 · 0 comments
Open

Use ChainMap to handle overriding default kwargs #622

TomNicholas opened this issue Feb 28, 2024 · 0 comments

Comments

@TomNicholas
Copy link
Contributor

TomNicholas commented Feb 28, 2024

I just learned what collections.ChainMap does, and one of the examples of its use is to manage overriding default keyword arguments, e.g.

from collections import ChainMap

class CRMUser(User):
    def __init__(self, name, component, **kwargs):
        defaults = {"user_id": next(component.user_id), "role": "read"}
        super().__init__(name, **ChainMap(kwargs, defaults))

where kwargs is optional, and if provided a key in kwargs will end up overriding the corresponding default key in defaults.

We have this problem a lot in xGCM, with default kwargs that we allow the user to override. This pattern might simplify handling the options.

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

No branches or pull requests

1 participant