the annoying, verbose self

Ton van Vliet sheep.in.herd at green.meadow
Sat Nov 24 09:42:14 EST 2007


On 24 Nov 2007 13:56:37 GMT, Marc 'BlackJack' Rintsch <bj_666 at gmx.net>
wrote:

>On Sat, 24 Nov 2007 14:09:04 +0100, Ton van Vliet wrote:
>
>> On 24 Nov 2007 08:48:30 GMT, Marc 'BlackJack' Rintsch <bj_666 at gmx.net>
>> wrote:
>> 
>>>On Sat, 24 Nov 2007 09:12:34 +0100, Ton van Vliet wrote:
>>>
>>>> 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!)
>>>
>>>And here lies the problem:  The compiler decides at compile time which
>>>names are local to a function and there is no equivalent of a record
>>>definition to make that decision.
>> 
>> The whole clause following 'using self:' could be seen as the record
>> definition: all attribute assignments/bindings in there should be
>> prefixed with 'self' (if not already existing in the self namespace?)
>> and thereby become instance attributes, which outside the 'using self'
>> clause still need 'self.' as the (namespace?) prefix.
>
>So::
>
>    def meth(self):
>        using self:
>            tmp = raw_input('Enter age: ')
>            age = int(tmp)
>
>becomes::
>
>    def meth(self):
>        using self:
>            self.tmp = self.raw_input('Enter age: ')
>            self.age = self.int(tmp)
>
>Binding `tmp` unnecessarily to the object and trying to get `raw_input()`
>and `int()` from the object.  Ouch.  :-)

Absolutely.

However, I was more thinking in terms of attributes only, like this:

    def meth(self):
        using self:
            self.tmp = raw_input('Enter age: ')
            self.age = int(self.tmp)

and for the methods check whether there *is* a self.<method> defined
in the class (or even within the 'using' clause), then use it, or else
leave it as it is.

It would require more 'lookup' work, but we're talking 'readability'
here (which would btw not bring much in the example you give ;-)

It would boil down to choice: explicit/speed vs implicit/readability

-- 
ci vediamo
Ton



More information about the Python-list mailing list