[Python-ideas] Mitigating 'self.' Method Pollution
ben+python at benfinney.id.au
Sat Jul 11 10:33:19 CEST 2015
Michael Hewitt <michael at hewitts.us> writes:
> Is anyone actually going to argue that the first version is cleaner
> and more readable than the second? All I see when I read the first is
> 'self', 'self', 'self' -- my son's exact words to me last night.
That's a pity. You can choose a different word if you like.
> As far as maintainability, the author of the first version must
> repeatedly make the same decisions over and over regarding how to
> scope each variable reference as he/she types 'y', 'gravity', and
Another way to say that is that the author must *explicitly
communicate*, to the reader, the namespace of each name within the
don't make the mistake of attempting to optimise time spent typing in
code, versus time spent comprehending the code as a reader. The latter
far outweighs the former, and is the preferred target for optimisation.
> I can tell you that the mental overhead of [writing] this is taxing my
> son & is the cause of many painful mistakes -- just forgetting a
> single 'self.' prefix on one of the above field references can waste a
> significant amount of time.
With respect, I doubt that the time spent on that is anywhere near the
time which would be spent trying to diagnose bugs caused by implicit
You're aware of the cost in writing explicit names, because you're
paying it now. You are, I testify from painful experience in languages
other than Python, saving a much greater amount of time in hunting bugs
caused by ambiguously-written code.
> If the 'self.' prefix really does make code clearer, then we should do
> away with the 'global' scope declaration as well as automatic local
> scoping and require prefixing of all Python variables with 'self.',
> 'global.' or 'local.'. My mind becomes numb thinking about writing
> such code.
I share your abhorrence, that would be an unwarranted step. Who is
advocating taking that step? No-one in this thread that I can see.
If someone advocates “require prefixing of all Python [names] with
‘self.’, ‘global.’, or ‘local.’” within my earshot, I'll join you in
criticising the foolishness of such an idea.
> To me, the existence of the keyword 'global' for automatically scoping
> subsequent variable references is a strong argument that a similar
> 'self' scoping mechanism is called for as well.
You haven't presented anything compelling in support of that. The latter
doesn't follow at all from the former.
> And, for folks who still prefer to prefix all their field references
> with 'self.', the proposal in no way prevents them from doing so. It
> merely allows the rest of us to be a bit less wordy and more pithy in
> our code.
Python requires explicit declaration of the namespace for names. That
protects me from ambiguities that would otherwise be very common in
other people's code. I appreciate that and do not want it threatened.
\ “I distrust those people who know so well what God wants them |
`\ to do to their fellows, because it always coincides with their |
_o__) own desires.” —Susan Brownell Anthony, 1896 |
More information about the Python-ideas