the annoying, verbose self

J. Clifford Dyer jcd at sdf.lonestar.org
Thu Nov 22 14:50:24 CET 2007


On Thu, Nov 22, 2007 at 10:13:46AM +0100, A.T.Hofkamp wrote regarding Re: the annoying, verbose self:
> 
> On 2007-11-22, Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au> wrote:
> > On Wed, 21 Nov 2007 15:51:56 -0800, braver wrote:
> >
> >> Is there any trick to get rid of having to type the annoying,
> >> character-eating "self." prefix everywhere in a class?
> 

You're tilting against windmills.  This has been gone over many times.  There are very valid reasons for the explicit self, which I understood well enough when I read about them to stop complaining about self, but not well enough to be able to justify to someone else.  Sorry for that.  But since Alexy likes finding old threads that address his concerns, maybe he'll hunt them down, himself.  

> You got this highly flexible language, very good for rapid programming, no more
> clutter from block brackets and variable declarations, and now this 'self' pops
> up.
> 
> So much overhead....
> 
> Can the computer not decide by itself what I want stored in the object? It can
> read my code too!
> 

But not every variable declared inside a class needs to be assigned to self.  Make judicious use of non-self variables, if you don't want to see self everywhere.

> 
> > Oh I know! It' uch a pain. Sinc writing a hug cla lat wk, I'v had a 
> > trribl hortag o lowrca S E L and F charactr. It mak writing vry annoying.
> 
> Yes annoying isn't it?
> 
> Last week I was programming in C++ again, and now I have this terrible sur-plus
> of lowercase T H I and S letters. I don't understand how they do it (I used to,
> but not any more).
> 
> Maybe we should setup an exchange program so everybody can trade letters with
> each other.
> 
> 
> >> Sometimes I avoid OO just not to deal with its verbosity.
> >
> > There are other values than brevity. In fact, brevity is one of the less 
> > important values.
> 
> NO! You got it all wrong here! It is not about brevity, it is about SPEED.
> 

if you're looking for speed of typing, just type a one character self equivalent, like:

class q(object):
    def method(;, arg):
	    ;.var = 3

Then when you're done coding: :%s/;/self/g

> With less letters, there is less to read, so it can be read faster, so it must
> be better!
> 

Ah.  Speed of reading.  In fact, speed of reading is connected to clarity, not brevity.  If you have to keep looking back to some declaration section to remember if a given variable is tied to self, it's going to slow down your comprehension speed.  the written chinese language is far more concise than japansese, but it's faster to skim japanese, because of the three alphabet system.  Most of the meaning is contained in kanji, while the hiragana supplies disambiguation and grammatical specifiers, so you know you can skip past the hiragana and still retain the basic meaning.  Having self used by convention means that once you get used to seeing it, you don't need to read it any more.  It would really mess with python coders, though, if each person made up their own self-equivalent.  You write "s", I write "self", Larry Wall starts coding python and uses "my".  Then we'd all have to actually pay attention, and reading really would slow down.  That's probably why self seems to be the most consistently held convention in python.  Occasionally, someone will use cls, but that's for a different context, where you really do want people to slow down and realise that something different is going on.

> Just like "if x:" is MUCH BETTER than "if x != 0:"
> 
Only when "if x:" is what you mean.  The semantics are different:

py>>> x = ''
py>>> if x: print "x"
py>>> if x != 0: print "x != 0"
x != 0

> 
> The thing that should be fixed imho, is that the brain of some of us tries to
> deduce higher levels of abstractions from what is essentially a very long line
> of simple instructions.
> 

Sorry, no entiendo.  What needs to be fixed here?

> 
> >> But things grow -- is there any metaprogramming tricks or whatnot we can
> >> throw on the self?
> >
> > Oh yeah, that's just what I like to see! Complicated, brittle, hard to 
> > debug, difficult to understand metaprogramming tricks in preference to a 
> > simple, easy-to-read naming convention.
> 
> Maybe we should state first which variables we want to store in self, and then
> have a meta-programming hook that automatically puts assignments to those
> variables into the object.
> And while we are at it, maybe we should also state the type of data we want to
> put in it. That should help too.
> 
> Now that would be progress.
> 

I hope you are taking the piss.

> 
> Ah well, I guess we will have to wait a while before that happens.....
> Albert

Oh God.  You are taking the piss.  IHBT.

<sigh>



More information about the Python-list mailing list