[Python-Dev] Self in method body
steve at pearwood.info
Mon Dec 8 23:52:20 CET 2008
On Tue, 9 Dec 2008 08:55:21 am Filip Gruszczyński wrote:
> There is a large discussion on python-list about Guido's article
> about new self syntax, therefore I would like to use that to raise
> similar question: self in the body. Some time ago I was coding in
> Magik language
> (http://en.wikipedia.org/wiki/Magik_(programming_language), which is
> dynamically typed and similar to Smalltalk and actually to Python too
> - although the syntax is far less appalling. As you can see in the
> examples, defining methods is very similar to what Guido proposed in
> his blog, though you don't provide the name of the argument, but the
> name of the class. Then you just precede attributes with a '.', which
> is 4 letters less than self. And, well, this rocks ;-)
> It is really not a problem to type 4 letters (well, six with a coma
> and a space) in the signature, but it takes a lot of time to type all
> those selfs inside the function's body.
For some definition of "a lot".
I've just grabbed a random, heavily OO module from my own code library.
It has 60 instances of "self", or 240 characters, out of 18,839
characters in total (including newlines). Removing self will decrease
the number of my keystrokes and the amount of pure typing time
(excluding thinking time, debugging time) by about 1.2%. I don't call
that "a lot" -- it's actually quite small. And it becomes vanishingly
trivial when you factor in that most of the time spent programming is
not typing but thinking, testing, debugging, etc.
Doing the same calculation for BaseHTTPServer.py and SimpleHTTPServer.py
in the standard library, I get 1.9% and 2.0% respectively.
> This could really save a lot of code, while attributes are still
> easily distinguishable.
I don't think so.
More information about the Python-Dev