total idiot question: +=, .=, etc...

Neel Krishnaswami neelk at brick.cswv.com
Thu Jun 24 03:56:41 CEST 1999


In article <m3aetr2yk9.fsf at atrus.jesus.cam.ac.uk>,
Michael Hudson  <mwh21 at cam.ac.uk> wrote:
>behrends at cse.msu.edu (Reimer Behrends) writes:
> 
>> Personally, I can easily live without a construct that I can use on
>> average once every three screens of code or so. On the other hand, I
>> _am_ getting pretty sick of having to type self.whatever countless
>> times, where pretty much any other language allows me to discard the
>> "self." part. Not only is it annoying to type, it also reduces the
>> readability of OO code a lot, much like the gratuitious use of
>> punctuation characters as variable prefixes in Perl does.
>
>I sometimes think this when writing Python; but in my summer job which
>I've just started, I'm frequently faced with large screenfulls of C++
>and not being able to tell whether a given variable is local, static,
>class or global at a glance is mightily annoying. 

I don't mind writing self.foo = bar, for the reason you describe, but
I know I hate having to write self.__class__.member = foo in order to
keep inheritance from breaking:

class Foo:
    x = 9
    def munge_x(self, x):
        Foo.x = x

class Bar(Foo):
    x = 12

>>> a = Foo()
>>> a.munge_x(15)
>>> print a.x
15

>>> y = Bar()
>>> y.munge_x(15)
>>> print y.x
12

But if I change Foo to:

class Foo:
    x = 9
    def munge_x(self, x):
        self.__class__.x = x

and try the same thing, I get the more intuitive:

>>> a = Foo()
>>> a.munge_x(15)
>>> print a.x
15

>>> y = Bar()
>>> y.munge_x(15)
>>> print y.x
15

It's just that writing self.__class__ is so *ugly*! Everything else
in Python is beautiful, too, which makes this stand out even more. 


Neel




More information about the Python-list mailing list