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

Just just at xs4all.nl
Sat Jan 11 15:30:12 EST 2003


In article <slrnb20pme.ptc.grante at localhost.localdomain>,
 Grant Edwards <grante at visi.com> wrote:

> In article <ThZT9.98$B62.32373 at news.uswest.net>, Kevin Altis wrote:
> 
> > The most common is probably 's'. However, it makes it more difficult for
> > other people to read your source if don't use 'self', so it isn't advisable.
> > You might consider binding a "macro" to type out "self." if typing the
> > characters bothers you
> 
> I don't know about the OP, but I don't care about the typing.  That's NRE.
> It's the reading that's a problem.  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.
> 
> Using local variables helps...

Yup. To me the above looks like someone is using instance variables as 
if they are globals and I've seen beginners do that many times. IMHO, 
using instance variables for actual state, and passing other values as 
arguments to methods is often much better style. And in cases you really 
*do* need lots of instance variables, temporarily binding them to local 
vars can help. Sometimes bundling several variables in a tuple helps, as 
in this snippet from a module of mine:

    def inverse(self):
        "Return the inverse transform."
        if self.__affine == (1, 0, 0, 1, 0, 0):
            return self
        xx, xy, yx, yy, dx, dy = self.__affine
        det = float(xx*yy - yx*xy)
        xx, xy, yx, yy = yy/det, -xy/det, -yx/det, xx/det
        dx, dy = -xx*dx - yx*dy, -xy*dx - yy*dy
        return self.__class__(xx, xy, yx, yy, dx, dy)

Just




More information about the Python-list mailing list