[Python-Dev] OT: style convention: self vs. _ in new Norvig's book

Peter Norvig pnorvig@google.com
Mon, 21 Jan 2002 10:16:51 -0800


Wow; I didn't expect this to generate such a response.  But I did post
the code far before it was ready and put the "I could be talked out of
it" there for a reason. So, thank you for your feedback!  My reactions:

4 spaces: OK.=20

I have no strong feelings on that, and I think its just an accident of
the way my emacs was configured that I started using 2 spaces. I agree
that I should make it easier for other people to edit my code, so I'll
switch to the default.

self: OK, I'll try it.=20

My rationale was: I'm used to Java, where self is usually spelled '',
and I figured '_' was the next best thing. I find it much nicer to read
because 'self' is too intrusive; I want something that disappears.=20
Compare:

	_.x, _.y, _.z =3D x, y, z
	self.x, self.y, self.z =3D x, y, z

Besides saving 9 characters, I find that the first line I can read at a
glance, ignoring the _, while the second I have to look at more
carefully. I also like the symmetry of _._ in _._private_slot.  However,
I recognize I'm doing this as an outsider to the language without much
experience reading/writing it. If it is really true that using '_' would
be seen as a change to the language and not a personal quirk, then I
agree that I shouldn't do it.  The first hint I had of this was when I
saw something on comp.lang.python (I forget the details) suggesting that
an automated tool look for methods with first argument 'self'. So I'll
try 'self' for a while, and hope I learn to like it (and learn to read
the second sample line above in one glance).  If I don't, I'll write
here and give you all another chance to innundate me with reasons why I
should.

-Peter

PS - Getting a personal request from Guido reminds me of the time I was
at a conference and John McCarthy walked up to the booth of one of the
Lisp vendors and said in his usual direct fashion "I hear you have a new
version. You should send me one".  The booth bimbo had no idea who
McCarthy was and politely suggested he pay for a copy.  Then someone in
the booth with a little more experience came over and said "That's ok --
it's his language, he can have whatever he wants."


Guido van Rossum wrote:
>=20
> > http://norvig.com/python/python.html
> >
> > Peter Norvig is about to supply
> > Python versions of the algorithms with
> > the 2nd edition of his AI: A Modern Approach.
> >
> > So far, so good. In the section about
> > coding convetions he says:
> >
> > =A6In general, follow Guido's style conventions,
> > =A6but I have some quirks that I prefer (although I could be talked o=
ut of them):
> > ...
> > =A6* _ instead of self as first argument to methods: def f(_, x):
> > ...
> >
> > I'm perfectly aware that the 'self' thing it is just a convetion,
> > OTOH much of the cross-programmer readability
> > of code relies on such convention.
> >
> > It is good, bad or irrelevant to have such
> > an authoritative book (although about AI not
> > Python directly) adopting such a line-noisy
> > convention?
> >
> > Maybe nobody cares, but I preferred not to
> > let this go unnoticed. Someone who cares
> > could try to discuss the issue or make it
> > apparent to Mr. Norvig.
> >
> > Opinions?
> >
> > regards, Samuele Pedroni.
>=20
> Peter:
>=20
> My apologies for butting in here without doing full research.  I don't
> know how you reached this set of conventions, so maybe you've got a
> very good reason; but I don't see it on your webpage.
>=20
> Two of those coding conventions look really ugly to me: 2-space
> indents and _ for self.  I think the code will look horrible!
>=20
> I think everyone should be able to make their own style choices, but I
> ask you to reconsider.  If you have to reconsider one, I would beg you
> to use 'self' like everybody else.  The _ name is already overloaded
> with multiple meanings in the Python community: it's a shorthand for
> the last evaluated expression in interactive mode, and some people use
> it as a dummy variable to assign uninteresting results to.
>=20
> Almost the entire Python community is happy with 4-space indents; if
> you're worried about your lines getting too long, that's usually a
> hint that your code can be restructured in a way that's easier on the
> reader's eye/mind anyway.
>=20
> --Guido van Rossum (home page: http://www.python.org/~guido/)

--=20
_____________________________________________________________________
Peter Norvig, Director of Machine Learning, Google, http://google.com
pnorvig@google.com,  Voice:650-330-0100 x1248,  Fax:650-618-1499