newbie: self.member syntax seems /really/ annoying
Michele Simionato
michele.simionato at gmail.com
Wed Sep 12 11:12:15 EDT 2007
On Sep 12, 4:43 pm, Charles Fox <charles.... at gmail.com> wrote:
> Thanks guys -- yeah these two stategies (short s.varname; and explicit
> rescoping, a=self.a etc) are more or less what I was using. That's
> still kind of annoying though.
>
> The s.varname approach still makes numerical code much harder to read.
>
> I had a nasty bug with the boilerplate approach when forgetting to
> reassign some of the variables back to members (self.a=a). And that's
> a lot of boilerplate you need -- I thought the python way was to
> minimize redundant code? (Ditching header files and curley brackets
> was a main reason for me coming here).
You still can play with dictionaries, for instance:
def __init__(self, a, b, c):
vars(self).update(locals())
correspond to
self.a = a
self.b = b
self.c = c
(and actually also to self.self =self).
> I see the argument for making self explicit -- what would be wrong
> with just .a instead of self.a though? That's still explicit but much
> easier to read. (I think I've seen that somewhere else, is it C#?)
This has been proposed many times. But self is handy because you can
give
a different name to it: for instance it becomes cls when you are
inside a classmethod.
Michele Simionato
More information about the Python-list
mailing list