History of 'self' and why not at least 'my'?
Steve Holden
sholden at holdenweb.com
Fri Jan 11 08:54:55 EST 2002
"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
news:Xns91937D050795duncanrcpcouk at 127.0.0.1...
> Giorgi Lekishvili <gleki at gol.ge> wrote in news:3C3F4D4B.5922ABED at gol.ge:
>
> > I would like rather to have something akin of Pascal's or VB's 'with',
> > e.g., with self:
> > bla1,
> > bla2
> > ...
> >
> > Grtz,
> > Giorgi
> >
>
> The FAQ describes why a "with" statement as you describe it doesn't work
in
> Python: see http://www.python.org/doc/FAQ.html#6.31
>
> However, what it doesn't really cover is that a common use of "with" is to
> reduce a complicated reference to an object:
> e.g. with obj[expression].field[exp].etc:
> a = 5
> b = 6
>
> If this is what you are trying to do, then Python already has a statement
> to handle this, namely 'assignment':
> w = obj[expression].field[exp].etc
> w.a = 5
> w.b = 6
> The same technique can be used at a local level to reduce the overhead of
> typing self:
> s = self
> s.a, s.b, s.c = 1, 2, 3
>
I've added the first point to FAQ 6.31, but not the second. While it is
true, it would seem a little perverse to go to the lengths of writing
def somemethod(self, *args):
s = self
s.a, s.b, s.c = 1, 2, 3
when you could just as easily write
def somemethod(s, *args):
s.a, s.b, s.c = 1, 2, 3
I know that this ignores the standard (conventional) reference name, but
then so does the former suggestion if the references to self are separated
by significant distance.
regards
Steve
--
http://www.holdenweb.com/
More information about the Python-list
mailing list