the annoying, verbose self

Patrick Mullen saluk64007 at
Sat Nov 24 20:10:18 CET 2007

On 24 Nov 2007 13:56:37 GMT, Marc 'BlackJack' Rintsch <bj_666 at> wrote:

> 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 =
> Binding `tmp` unnecessarily to the object and trying to get `raw_input()`
> and `int()` from the object.  Ouch.  :-)

Yes, that's no good.  So you would write it like so:

def meth(self,*args):
    tmp = int(raw_input('Enter age:'))
    using self:
        age = tmp

Still an unnecessary lookup on tmp though :)  And it would be useless
to use it for one assignment, the idea is to eliminate all the typing
with this:

self.var1 = 5
self.var2 = "a value"
self.var3 = stuff
self.var4 = [2,54,7,7]
self.var5 = "dingaling"
self.var6 = 6.4
self.var7 = 1
self.var8 = False
self.var9 = True

Of course that "self.var3 = stuff" under the using would result in a
bad lookup for "stuff", but the programmer who wanted to use this
would have to realize this and try to avoid it.

I have been known from time to time, for long assignments such as
this, to turn a string with keys and values into a dictionary, and
then update __dict__ with that dictionary, hehe.  "var1,5;var2,"a
value";var3,stuff"  Not the best practice but the fastest to type.
Sometimes I actually use a dictionary, but typing all of the quotes
for the keys gets old.

If there were a "using" or if the with statement would handle
something like this, I wouldn't use it.  "s." is only 2 characters.  I
saw chained dots mentioned.  Chained dots are 2 characters.  Why are
we still discussing this?  "s." is the answer, or pulling the
attributes into local vars if you are going to use them many times, to
save lookup.  This is not a band-aid, this is an actual valid
programming technique.  There is more to programming than typing...

Self is never going away, most python programmers generally like or
aren't bothered by it, if you are new to the language try to get used
to it, if it's too bothersome you can use one of the hacks or try
other languages.  I don't mean to be snobby either, one language does
not fit all.

More information about the Python-list mailing list