Last active
August 15, 2024 18:55
-
-
Save giovaneliberato/ca78d5adf0861d851bc9c98c411b04a1 to your computer and use it in GitHub Desktop.
Lombok's builder-like class made with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Builder(object): | |
_fields = None | |
_to_build = None | |
def __init__(self, to_build): | |
self._fields = [] | |
self._to_build = to_build | |
def _add_field(self, name): | |
def inner(value): | |
self._fields.append((name, value)) | |
return self | |
return inner | |
def __getattr__(self, name): | |
return self._add_field(name) | |
def build(self,): | |
built = self._to_build() | |
for field, value in self._fields: | |
setattr(built, field, value) | |
return built |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you show an example of using this builder class