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

@inject_fields decorator for __init__ #13

Closed
smarie opened this issue Sep 26, 2019 · 0 comments
Closed

@inject_fields decorator for __init__ #13

smarie opened this issue Sep 26, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@smarie
Copy link
Owner

smarie commented Sep 26, 2019

Feature 3

user writes his own but does not want to write all the argument names so he could use a special variable fields in the constructor, and anywhere in his constructor do fields.assign() or fields.init().

        @inject_fields(height, color)
        def __init__(self, fields):
            fields.init()

pros:

  • you can use autocompletion so it is fast to type the decorators
  • you only declare the fields that you need, in the order that you like
  • you control when init happens
  • you can easily debug what happens during field initialization

cons:

  • it does not seem possible to have @inject_fields work without arguments, or even with the class as argument, to say "use all fields from class", except if we allow introspection to be used. Indeed, at the time where the __init__ method is decorated, it is not yet bound to the class. EDIT actually it is possible: we can create a non-data descriptor on the class (this was inspired by this link).

Originally posted by @smarie in #2 (comment)

@smarie smarie added the enhancement New feature or request label Sep 26, 2019
@smarie smarie closed this as completed in dd020dc Sep 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant