the annoying, verbose self
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Sat Nov 24 08:56:37 EST 2007
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. :-)
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list