"self" - a python wart or "feature"

John Roth johnroth at ameritech.net
Mon Feb 17 18:33:28 EST 2003


"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
news:Xns93258C65B188Eduncanrcpcouk at 127.0.0.1...
> "John Roth" <johnroth at ameritech.net> wrote in
> news:v51m6ufe8pkk15 at news.supernews.com:
>
> > To give you an example of the issues with local variables,
> > look at the "global" statement. It's there specifically to
> > insure that you can assign to a variable at module level (that is,
> > outside of the function definition.) If you didn't have an explicit
> > "self," you'd have to do the same thing for classes.
>
> I wouldn't want to lose the explicit self in the argument list (there
are
> just too many good reasons why it is to be preferred), but there could
be
> an argument for adding a "global" equivalent to make self optional
when
> referring to instance variables:
>
>    def aMethod(self, x):
>        instance p, q, r
>        p = q * x + r # Equivalent to: self.p = self.q * x + self.r
>
> The benefit would be improved readability in expressions with a lot of
> instance variables.
>
> (The word 'self' isn't intended to be special here, instance would
simply
> mean the specified names are attributes of the first parameter.
Assigning
> to the first parameter in a method using instance would be either
illegal
> or undefined.)
>
> One might then consider a 'local' keyword whose use would mean that
all
> local variables in the current scope must be listed in one of
'global',
> 'instance' or 'local'. Any other names would cause an error at compile
> time.
>
> Of course it might be better simply to get unittest to generate
warnings if
> there are any classes/functions/methods not exercised by tests. :-)

I'm not sure what this has to do with the rest of the topic,
but there is at least one coverage analyzer (pycover.) And it
will run unittest just fine, thank you. It has other problems, but
still...

John Roth
>
> --
> Duncan Booth
duncan at rcp.co.uk
> int month(char
*p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
> "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?






More information about the Python-list mailing list