-
-
Notifications
You must be signed in to change notification settings - Fork 385
treatment of private attributes is obscure, nearly undocumented, perhaps not ideal in all cases #171
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
Comments
As a little history lesson, the origin of the behavior is hynek/characteristic#6. |
To be clear - especially given that history :) - I find this behavior entirely intuitive, and have not previously found it to be problematic. However, having had it pointed out to me that it's confusing, I found it reasonable to expect some other behavior unless you have a view of private attributes with very specific nuances. |
This came up recently while discussing My initial thought was that having private arguments in However it's also true other languages can have other, private constructors, but Python can't. Also for normal functions, users can define additional private functions in addition and put the private arguments there; not so with I'm guessing kw-only args in Python 2 are emulated using **kwargs? It'll obfuscate the init signature a little, but that's what you get for using legacy Python <trollface.jpg> |
fixed in #370 |
Some folks, presented with the
attr.s
API, make the entirely reasonable inference that this:is a class you should use like this:
This is further exacerbated by the fact that
Foo
'srepr
actually shows you exactly that string.The only example in the docs that I can find is the code example at the bottom of the
attr.s
API doc.This behavior should be thoroughly documented and explained, and, um, maybe tweaked a little bit, since there are some issues with it. As it stands now, for example, there's no publicly-named attribute for API doc generation tools, or code-completion things, to pick up to present as the constructor argument.
If I could take a stab at the rationale for this behavior, it's something like this:
Except, in Python 3, this isn't true any more, because the advent of keyword-only arguments means you can't pass the argument without actually having to type in an
_
in a namespace you don't own, which means it's totally valid to have internal "don't pass me this in the constructor" state. IMHO this is great - it's a way to force public users through a class-method constructor or factory function without hiding the type itself (and all the methods on it) or resorting to any gross shenanigans to make the constructor literally private. So it turns out I really would like "private attributes" that don't show up in the constructor either! (And of course, at the metaprogramming level where Attrs itself lives, the kwarg-only behavior can be emulated on python 2.)The text was updated successfully, but these errors were encountered: