Use empty string for self

paullanier at gmail.com paullanier at gmail.com
Tue Feb 28 18:54:06 EST 2006


It seems that lots of people don't like having to prefix self. in front
of instance variables when writing methods in Python.  Of course,
whenever someone suggests doing away with 'self' many people point to
the scoping advantages that self brings.  But I hadn't seen this
proposal when I searched so I thought I'd throw it out there.  Maybe
it's already been thrown out but I like it.

The issue I have with self. is that is makes the code larger and more
complicated than it needs to be.  Especially in math expressions like:
self.position[0] = self.startx + len(self.bitlist) * self.bitwidth

It really makes the code harder to read.  On the other hand,
eliminating the self. would create other issues including readability
with regards to which vars are instance vars and which come from
somewhere else.

But what if we keep the '.' and leave out the self.  Then the example
looks like:
.position[0] = .startx + len(.bitlist) * .bitwidth

The 'self' is implied but the scoping rules don't change and it's still
clear when reading it that they are instance variables.  We can keep
the self in the method header (or not) but that is really a separate
issue.

Any comments?  Has this been discussed before?




More information about the Python-list mailing list