You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classFoo(object):
def__init__(self, x):
self.x=xself.y='bla'classBar(Foo):
def__init__(self, x):
super(Bar, self).__init__(x)
self.z=self.foo() # this is what I want to do with attrsdeffoo(self):
returnself.y
Personally I would like to be able to mix and match having the attr generated __init__ along with some user defined __init__, and be able to decide which is executed first. Is there currently any way at all to execute user defined initialisation other than not decorating your class with @attr.s?
@hynek I could use something similar to your __post_init__() as well, but I'd like the usual attr-provided __init__ to be called first. My use case is an object that takes a regex string as an arg and saves a compiled versions to a private attribute.
Currently I'm doing this with a classmethod constructor:
but this is less than ideal / breaks user expectations since the way to make an A instance is A.create(..) instead of A(..) (which gives you a half-built object).
I initially implemented a version using __init__ that (ab)used side effects in a validator to do the compilation, but then decided that was too hacky even for me.
I was kinda confused by signature of __init__here and here not taking any arguments, and the value being set in post_init, but I think that's specific to x = attr.ib(init=False)
Activity
hynek commentedon Dec 20, 2015
So the problem here is that when
zis defined,foodoesn’t exist yet.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:
would make sense?
econchick commentedon Dec 22, 2015
I think so - if
class Cinherited from another class, e.g.class B, can__post__access attributes defined inclass B?hynek commentedon Dec 23, 2015
I’m thinking that attrs will generate:
for you.
grahamegee commentedon Mar 4, 2016
Personally I would like to be able to mix and match having the attr generated
__init__along with some user defined__init__, and be able to decide which is executed first. Is there currently any way at all to execute user defined initialisation other than not decorating your class with@attr.s?wearpants commentedon Aug 18, 2016
@hynek I could use something similar to your
__post_init__()as well, but I'd like the usual attr-provided__init__to be called first. My use case is an object that takes a regex string as an arg and saves a compiled versions to a private attribute.Currently I'm doing this with a classmethod constructor:
but this is less than ideal / breaks user expectations since the way to make an
Ainstance isA.create(..)instead ofA(..)(which gives you a half-built object).I initially implemented a version using
__init__that (ab)used side effects in a validator to do the compilation, but then decided that was too hacky even for me.hynek commentedon Aug 18, 2016
Why would you say "but"? That's the whole point of post. :)
wearpants commentedon Aug 18, 2016
I was kinda confused by signature of
__init__here and here not taking any arguments, and the value being set in post_init, but I think that's specific tox = attr.ib(init=False)If we had:
I'd expect attrs to generate something like:
Sorry if I'm confusing things, been up since 4 AM for no good reason.
hynek commentedon Aug 18, 2016
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.
hynek commentedon Aug 21, 2016
Closing in favor of #68 which contains a more succinct description of what we came up with.