-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
Means to pass arguments to __attrs_post_init__ #180
Comments
The usual pattern (not just in attrs, but in Python) is to use class methods as alternate constructors.
You haven't showed us Foo, so presumably foo.json contains the values for a, b and c :) |
Yep, @Tinche is 💯 on this one. I’m closing this now, feel free to comment/ask if you have any further questions though! |
I oversimplified what I'm trying to do, which is actually some fairly complex initialization that uses the normal attrs initialization then adds more stuff, where the more stuff is done in attrs_post_init(). A little closer to what I'm trying to do is:
where a and b are defined with attrs, but various other attributes (possibly including c and d) need to be pulled from the binary image, and the object is intended to be immutable (frozen). In general, it seems like a way to pass args to attrs_post_init() would be useful for any number of things, not just what I'm doing right now. |
You should really look into using |
I must just be really dense. I still don't see how to do it that way for a frozen object. I need it to be frozen because it needs to be hashable. |
Untested example: @attr.s(frozen=True)
class Foo:
x = attr.ib()
@classmethod
def from_file(cls, path):
with open(path) as f:
return cls(x=f.read()) Gives you a very explicit way to instantiate Foo and at the same time makes it easy to test it since you can instantiate it with anything. |
I'd like to be able to use attrs_post_init() to support alternate means of initializing the object, based on extra arguments to the initializer. For my purposes it's fine if the normal attrs initialization is done first. For instance, I might want to write:
Perhaps only if an attrs_post_init() exits, and there are any arguments that can't be handled in the usual way, those arguments could be passed to attrs_post_init()?
I wouldn't object too much if the initialization of y as shown above didn't work if the a, b, and c attributes didn't have default values. On the other hand, perhaps it would be nice for an alternate initialization to work even if non-defaulted arguments weren't provided.
Perhaps there's a better way to do this which I've simply overlooked.
I could do something like:
but that can't be used if Foo is 'frozen'.
The text was updated successfully, but these errors were encountered: