-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Add ability to use self
within factories
#25
Comments
So the problem here is that when It would be possible to pass self into factories but they’d have to be functions which kind of defeats the purpose, right? I wonder if something like: @attr.s(post=True)
class C:
x = attr.ib(init=False)
def __post_init__(self):
"""
Called at the end of __init__.
"""
self.x = "whatever" would make sense? |
I think so - if |
I’m thinking that attrs will generate: def __init__(self):
self._x = None
self.__post_init__() for you. |
Personally I would like to be able to mix and match having the attr generated |
@hynek I could use something similar to your Currently I'm doing this with a classmethod constructor: @attr.s
class A:
some_regex = attr.ib()
@classmethod
def create(cls, *args, **kwargs):
self = cls(*args, **kwargs)
self._some_regex = re.compile(self.some_regex)
return self but this is less than ideal / breaks user expectations since the way to make an I initially implemented a version using |
Why would you say "but"? That's the whole point of post. :) |
I was kinda confused by signature of If we had: @attr.s
class A:
x = attr.ib()
def __post_init__(self):
assert hasattr(self, 'x') I'd expect attrs to generate something like: class A:
def __init__(self, x):
self.x = x
self.__post_init__() Sorry if I'm confusing things, been up since 4 AM for no good reason. |
heh yeah my current plan is what you'd expect. This but thread started in a different direction until it settled on this. :) I think I'll tackle it once immutably is in. |
Closing in favor of #68 which contains a more succinct description of what we came up with. |
A non-attrs example:
Or ideally with attrs:
or something like that.
DANKE.
The text was updated successfully, but these errors were encountered: