msg105161 - (view) |
Author: Lucian Ursu (LucianU) |
Date: 2010-05-06 20:00 |
I suggest that name mangling should not be recommended as a way of having private attributes. Instead, one underscore should be suggested as a signal that the attribute is private. This suggestion comes after discussing with some of the helpers from #python. Basically, they said that name mangling should never be used, so it would be appropriate to remove this recommendation from the tutorial. |
|
|
msg105164 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2010-05-06 20:31 |
Note that the opinion of people helping in #python does not necessarily (or frequently) match the one of core Python developers. That said, I agree with this particular suggestion. |
|
|
msg105191 - (view) |
Author: Skip Montanaro (skip.montanaro) *  |
Date: 2010-05-07 11:13 |
Maybe the wording should be changed, but name mangling serves a useful purpose. There are two definitions of "private" which seem to be a bit conflated in this section: * "private" as in, "this name is not part of the public API - use it at your own risk". * "private" as in, "I don't want someone to accidentally stomp on this attribute name when subclassing this class". I think it is valuable to mention both of these conventions in the tutorial for a couple reasons: * the tutorial is meant for people new to Python but with experience in other programming languages * the distinction between _a and __a is a bit subtle and not obviously similar to privacy features present in other languages. I work with a lot of C++ programmers who also write some Python (sometimes a lot of Python). It's clear at times that the distinction hasn't always sunk in. |
|
|
msg105240 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2010-05-07 22:35 |
Double underscore name mangling is for avoiding name clashes with base classes, not for 'private attributes'. This is such an advanced and rarely used feature that it hardly seems appropriate for the tutorial. |
|
|
msg105243 - (view) |
Author: Clovis Fabricio (nosklo) * |
Date: 2010-05-07 22:39 |
I help in #python and always suggest people to not use double-underscore name mangling when they mean private. I agree that name mangling shouldn't be on the tutorial at all. It misleads everybody coming from other languages into thinking that it means "private" like it does in java or C++. |
|
|
msg105261 - (view) |
Author: Fred Drake (fdrake)  |
Date: 2010-05-08 04:52 |
On Fri, May 7, 2010 at 6:35 PM, Terry J. Reedy <report@bugs.python.org> wrote: > This is such an advanced and rarely used feature that it hardly seems appropriate for the tutorial. The problem with just leaving it out is that learners stumbling over them in existing code (likely by trying to use something that looks like it should be there and getting an AttributeError) won't have a chance of knowing what the problem might be. I'd be fine seeing new uses discouraged, especially for new learners, but I think it's important that they be given a heads-up as well. -Fred |
|
|
msg105283 - (view) |
Author: Georg Brandl (georg.brandl) *  |
Date: 2010-05-08 12:54 |
The section titled "Private Variables" was updated fairly recently to be more in line with the Python philosophy. In its current form (which I verified to be present at e.g. http://docs.python.org/tutorial/classes#private-variables) I don't think it needs to be removed or revised (except for further clarification, of course.) If someone disagrees, I would like to see a patch (or a suggestion for new wording). |
|
|
msg112179 - (view) |
Author: Georg Brandl (georg.brandl) *  |
Date: 2010-07-31 18:26 |
No objections noted, closing. |
|
|