Is it advisable to replace 'self' with '_' or 'I' or something shorter?

Andrew Dalke adalke at mindspring.com
Sat Jan 11 15:41:23 EST 2003


Grant Edwards wrote:
 >  If you've expressions like
> 
>    (((a+b)/c)*(d-x))-y
>    
> they end up looking like
> 
>    (((self.a+self.b)/self.c)*(self.d-self.x))-self.y
> 
> I have a much easier time grokking the former.  In some cases you end up
> with lines 100+ characters long instead of 20 or 30.

Of course if you have small variable names then the self. dominates
the name.  However, more realistic code uses more descriptive names,
as in (from wave.py)

    self._soundpos = self._soundpos + len(data) // (self._nchannels * 
self._sampwidth)

(*uggh* I've haven't done much C++ code in >5 years and the // *still*
looks like a comment to me, instead of integer division.)

At other times the code is more easily read as smaller components

    scaled_value = (self.a + self.b)/self.c
    transformed = scaled_value * (self.d - self.x) - self.y


> Using local variables helps...

As in from difflib

         a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk


					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list