[Python-ideas] Mitigating 'self.' Method Pollution

Nikolaus Rath Nikolaus at rath.org
Sat Jul 11 21:10:15 CEST 2015

On Jul 11 2015, Steven D'Aprano <steve-iDnA/YwAAsAk+I/owrrOrA at public.gmane.org> wrote:
> On Sun, Jul 12, 2015 at 01:07:56AM +1000, Steven D'Aprano wrote:
>> So what makes attributes so special that we have to break the rules that 
>> apply to all other variables?
>>     No tags needed: builtins, globals, nonlocals, locals.
>>     Tags needed: attributes.
>> This isn't a rhetorical question
> The short answer is, sometimes they are special, and sometimes they 
> aren't.
> For these people, I believe, all those explicit selfs do is add visual 
> noise to the code, especially if the code is the least bit mathematical 
> or if there are well-known conventions that are being followed:
> class Vector:
>     def abs(self):
>         return sqrt(x**2 + y**2 + z**2)
>         # versus
>         return sqrt(self.x**2 + self.y**2 + self.z**2)
> We're told that programs are written firstly for the human readers, and 
> only incidentally for the computer. I expect that most people, with any 
> knowledge of vectors, would have understood that the x, y, z in the 
> first return must refer to coordinates of the vector. What else could 
> they be? The selfs are just noise. If we are writing for human readers, 
> sometimes we can be too explicit. 
> I don't intend to defend "Do What I Mean" attribute inference. I don't 
> believe that is practical or desirable in Python. But there's a middle 
> ground: Michael's suggestion, where we can explicitly declare that 
> certain names are attributes of self, and thereby decrease the visual 
> noise in that method.

Thanks for that excellent post. I hope it'll cause some people to
reconsider. Although there seems to be a flood of objection mails, most
of them seem more instintive than rational to me.


GPG encrypted emails preferred. Key id: 0xD113FCAC3C4E599F
Fingerprint: ED31 791B 2C5C 1613 AF38 8B8A D113 FCAC 3C4E 599F

             »Time flies like an arrow, fruit flies like a Banana.«

More information about the Python-ideas mailing list