the annoying, verbose self

Ton van Vliet sheep.in.herd at green.meadow
Sat Nov 24 03:12:34 EST 2007


On Fri, 23 Nov 2007 17:16:25 -0800, "Patrick Mullen"
<saluk64007 at gmail.com> wrote:

>Most of the time self doesn't bother me in the slightest.  The one
>time it does bother me however, is when I am turning a function into a
>method.  In this case, often I have many local variables which I
>actually want to be instance variables, so I have to add self to all
>of them.  Of course, this is going to cause me some grief no matter
>which language I am using.  If it was enough trouble, it wouldn't be
>hard to make a filter that converts my code automatically.

Just bringing up something I sometimes miss from good-old Turbo-Pascal
here, which has the WITH statement to reduce the typing overhead with
(long) record/struct prefixes, used like:

with <prefix> do begin
    a = ...
    b = ...
end;

where all variables would be automatically prefixed with the <prefix>
(if present in the already available record definition!)

In your case where you have something like:

def somefunction(...):
    a = ...
    b = ...
    ...
    c = a + b

could (simply :-) become something like:

def somefunction(self, ...):
    using self:
        a = ...
        b = ...
        ...
        c = a + b

so only one line extra and an indentation shift.

Of course the interpreter would have to be (much) smarter here, since
the <prefix> (in this case just a simple 'self', but it could be more
complex, e.g. some module.levels.deep) is not necessarily defined in
advance.

Since 'with' is already in use, another keyword (e.g. 'using') would
be needed

I guess speed would also be a major issue, but readibility would gain
from it (IHMO :-)

-- 
Ton



More information about the Python-list mailing list