the annoying, verbose self
Colin J. Williams
cjw at sympatico.ca
Sun Nov 25 08:01:43 EST 2007
Steven D'Aprano wrote:
> On Fri, 23 Nov 2007 23:38:24 +0000, BJörn Lindqvist
> wrote:
>
>> 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.
>
> -2 Readability counts, and your example is a lot less readable.
>
> For your example to be even *slightly* readable, you have to fill the
> expression with excessive whitespace. A little bit of whitespace is good.
> Too much breaks the flow of the expression and hurts readability.
>
> Or perhaps I should say:
>
> T o o m u c h b r e a k s t h e f l o w . . .
>
>
> You write: math.sqrt(.x * .x + .y * .y + .z * .z)
>
> which to my eyes has too much whitespace, but the alternative is worse:
> math.sqrt(.x*.x + .y*.y + .z*.z)
>
> and this is positively painful to try to read, it looks like line-noise:
> math.sqrt(.x*.x+.y*.y+.z*.z)
>
>
>
> The correct solution to your example is to get rid of the attribute
> lookups from the expression completely:
>
>
> def abs(self):
> x, y, z = self.x, self.y, self.z
> return math.sqrt(x**2 + y**2 + z**2)
>
>
> It's probably also faster, because it looks up the attributes only once
> each, instead of twice.
>
>
Alternatively, as someone else
suggested, an analogue of the Pascal "with"
could be used:
def abs(self):
with self:
return math.sqrt(x**2 + y**2 + z**2)
As has already been pointed out, "with"
has been pre-empted (unfortunately,
in my view) for another purpose.
This form could be generalized to "with
aa" where aa is any object with
attributes accessible with the z= aa.z
or aa.z.= z style.
This should not apply to special names,
such as __add__ etc.
Colin W.
>
More information about the Python-list
mailing list