On 25/07/2015 22:08, boB Stepp wrote:
After having a long discussion with my wife on her user requirements,
I am convinced that an OO approach is required.  Which is just as well
as that has been one of my next areas of learning to do.  I am
currently reading "Python 3 Object Oriented Programming" by Dusty
Phillips, which so far seems to be a reasonably good text.  This has
led me to the subject line topics.

 From my understandings to date:

1) A single underscore is used conventionally for a "throw-away"
variable, such as a loop index for which the index value is not
actually used in a subsequent calculation.

It is not a convention, it is inbuilt. It is very useful as linter type tools don't complain about you defining something but not using it.

2) _name is used inside class methods to indicate that the
programmer's intention is that this name should only be accessed
internally by that particular class.  Other supposedly "adult" Python
programmers will attempt to respect this original intent if they use
my code.

Correct.

3) __name invokes Python's name mangling mechanism.  The intent of
this usage is to not allow subclasses of the class containing __name
to access this name, and to add additional emphasis to future users of
my code that this name is meant to be strictly hands-off.

4) name_ is used when one is "forced" to use one of Python's reserved
words as a name.

I don't know about reserved words but it is certainly used rather than override a built-in name.

5) __name__ is meant to be used only by the creators of Python for
their special built-in methods, such as __init__, __new__, etc.

Correct.


Are my understandings above correct or flawed?

For (3), it seems to me that one would normally be able to use the
simpler _name construction from (2).  What would be a best-practice
example of when name mangling *should* be used?

I'd be inclined not to bother yourself with this. I've never used it in the centuries that I've been writing Python, and somebody who is determined enough can work around it anyway owing to Python's dynamic nature.

Likewise, it seems that normally (4) should never be needed, though I
have a feeling that I have seen something in tkinter recently that
suggests some exceptions, but I cannot (yet) bring it to mind.

If you like a name enough and cannot think of a better alternative why not?


And for (5), surely I should never violate this one?  It seems that in
some future edition of Python they might add any particular __name__
that I might try to use presently in their future version of Python
(however miniscule that possibility might actually be).

Definitely.


Thanks!
boB


No problem. Once again my invoice is in the post, your cheque by return please :)

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to