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

The constructor should not use **kwargs to improve usability of the API #15

Open
devfoo-one opened this issue Apr 6, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@devfoo-one
Copy link
Member

Better, define a speaking constructor and add a class method from_arguments()

https://www.erol.si/2016/01/python-and-multiple-constructors/

@devfoo-one devfoo-one added the enhancement New feature or request label Apr 6, 2020
@devfoo-one
Copy link
Member Author

e.g.

class Span(object):
    def __init__(self, uid: int = None, begin: int = 0, length: int = None, text: str = ''):
        self.uid = uid
        self.begin = begin
        self.text = text
        if length is None:
            length = len(text)
        self.length = length

    @classmethod
    def from_arguments(cls, **kwargs):
        return cls(
            uid=kwargs.get('uid', None),
            begin=kwargs.get('begin', 0),
            length=kwargs.get('length', 0),
            text=kwargs.get('text', "")
        )

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