the annoying, verbose self

Arnaud Delobelle arnodel at googlemail.com
Sun Nov 25 16:05:58 EST 2007


On Nov 24, 10:55 am, jakub silar <jakub.si... at volny.cz> wrote:
> BJörn Lindqvist wrote:
> > On Nov 22, 2007 2:08 PM, Colin J. Williams <c... at sympatico.ca> wrote:
>
> >>bearophileH... at lycos.com wrote:
>
> >>>Alexy:
>
> >>>>Sometimes I
> >>>>avoid OO just not to deal with its verbosity.  In fact, I try to use
> >>>>Ruby anywhere speed is not crucial especially for @ prefix is better-
> >>>>looking than self.
>
> >>>Ruby speed will increase, don't worry, as more people will use it.
>
> >>>Bye,
> >>>bearophile
>
> >>I don't see this as a big deal, but
>
> > The big deal is that "self." occupies important horizontal screen real
> > estate. That is, it is usually not self in itself that is problematic,
> > but the overflowing lines is. Take this silly vector class for
> > example:
>
> > 1    class Vector:
> > 2        def __init__(self, x, y, z):
> > 3            self.x = x
> > 4            self.y = y
> > 5            self.z = z
> > 6        def abs(self):
> > 7            return math.sqrt(self.x * self.x + self.y * self.y +
> > self.z * self.z)
>
> > Line 7 is 82 characters long which is, if you care about the
> > readability of your code, a very real problem because the line is to
> > long. So you break it:
>
> > 7            return math.sqrt(self.x * self.x +
> >                               self.y * self.y +
> >                               self.z * self.z)
>
> > Better, but definitely not as nice as it would have been with a
> > shorter self prefix like you propose. And in my eyes, having to break
> > lines like this instantly makes the code much less attractive. There
> > is probably not a single language in existance in which wrapping lines
> > doesn't make the code uglier.
>
> > I also notice that the lines in your mail are nicely broken at about
> > column 45, probably because you are typing on a PDA or similar? In
> > those situations, where you require much shorter lines than the
> > standard 78 characters, for example when typesetting code examples for
> > a book, the problem with line breaks is even worse.
>
> >>suppose that the syntax were
> >>expanded so that, in a method, a dot
> >>".", as a precursor to an identifier,
> >>was treated as "self." is currently treated?
>
> > I like that a lot. This saves 12 characters for the original example
> > and removes the need to wrap it.
>
> > 7            return math.sqrt(.x * .x + .y * .y + .z * .z)
>
> > +1 Readability counts, even on small screens.
>
> Below is my coding standard - I'm lazy, even lazy to persuade
> comutinties into strange (imho) language syntax extensions.
>
>      class Vector:
>          def __init__(s, x, y, z):
>              s.x = x
>              s.y = y
>              s.z = z
>          def abs(s):
>              return math.sqrt(s.x * s.x + s.y * s.y + s.z * s.z)
>
> Admit that changing habits may be more difficult then to change a
> language syntax.
>
> Jakub
>
> occasional lamerish Python user

Well you're not the laziest.  Here's mine:

class Vector:
    def __init__(self, *data):
        self.data = data
    def abs(self):
        return math.sqrt(sum(x*x for x in self.data))

Hey I'm so lazy it's untested :)
However I've got standards: I wouldn't swap 'self' for 's'.

--
Arnaud




More information about the Python-list mailing list