-
Notifications
You must be signed in to change notification settings - Fork 53
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
Possibility to call custom initialization #13
Comments
JFTR attrs has a similar feature called There’s really no reason to special-case extra_args though. Just let them put on self get them from there. It’s more straight-forward, easily understandable, and I doubt the complexity around handling |
But it actually can be part of public API. Also dunder methods might look scary to users. Possible problem with using non-dunder method name is an occasional name clash. Possible solutions are:
In both cases a user can start the name with a single underscore if this is not a part of public API.
I am not sure what do you mean by "just let them put on self"? The only way to put something on self is to add a Also I don't think it will be difficult to add |
For reference, we had an epic bikeshed on that topic and decorators came up too: python-attrs/attrs#68 – I seem to remember that a well-known method had the least downsides – especially re subclassing. Re I’m using attrs syntax for simplicity here now, but you could do this: @attr.s
class C:
x = attr.ib()
parent = attr.ib()
tmp = attr.ib(init=False)
def __attrs_post_init__(self):
self.tmp = TempStorage()
self.tmp.link(self.parent) The advantage is that it doesn’t introduce any new concepts. If you don’t want that field to be there, make it private using an underscore. OTOH I learned the hard way that trying to pander to every single use case under the sun leads to regret and tears. :) You can always write your own Also this seems like a feature that can be added later if really necessary. |
Indeed a special name looks most simple, but I don't like a dunder for this purpose. Beginners tend to think about dunders as being "magic" (although they are not) and therefore hesitate to use them. |
But this is magic -- just as magic as `__init__`, which they will still
have to use. I would teach beginners plain classes first, and then point
out they can save some work if they are okay with a certain pattern, by
using `@dataclass`. So the dunder doesn't bother me.
…On Tue, Jun 6, 2017 at 10:25 AM, Ivan Levkivskyi ***@***.***> wrote:
Indeed a special name looks most simple, but I don't like a dunder for
this purpose. Beginners tend to think about dunders as being "magic"
(although they are not) and therefore hesitate to use them.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#13 (comment)>,
or mute the thread
<https:/notifications/unsubscribe-auth/ACwrMnavegr-HZ4mzTyPdl8NVCLhfMRbks5sBYucgaJpZM4NwTB5>
.
--
--Guido van Rossum (python.org/~guido)
|
@hynek: that really was a mega-thread! Thanks for the pointer. From reading the thread, I think an optional well-known entry point like So, I propose that (subject to name changes) we have the generated As far as extra parameters go: I'm not sure we can add it later, without changing the |
I'll leave this for Eric to decide. |
Yes, because everything else would end up in wonky guesswork. People can also call |
I've implemented a parameter-less I opened issue #17 to decide if we want/need to pass parameters to it. |
Imagine a situation with this class:
It should be possible to refactor this into:
A simple way to achieve this is to add code like this at the end of generated
__init__
:The text was updated successfully, but these errors were encountered: