A better self

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Thu Jul 11 13:29:46 EDT 2002


Gabe Newcomb <Gabe.Newcomb at noetix.com> wrote:
>It seems to me that, given that most folks here agree the concept of
>'self' is useful, we may as well stick with the word 'self' as is. It's
>the explicit version--it makes things clear, and if anyone's really
>dying to use the shortest identifiers possible, they might consider
>Perl.
>
>Yes that last statement was partially tongue-in-cheek, but seriously,
>readability is worth preserving IMO.

Using self is indeed the most readable most of the time.

However, there are situations, mostly in numerical computations, where it is
necessary to have, say, a dozen variables in one expression, and a dozen
statements containing such expressions and some other statements in one
method.  It is far more readable to have them all appear in one screen in a
form that resembles written equations on paper, than to dogmatically follow
a convention in a particular programming language.  For anyone spending a
few weaks sorting out these equations and several days making sure that they
are correctly represented in the program, it is no big deal to notice that _
is used in place of self in this situation.

Just like the shortest programs are not always the most readable programs,
programs that follow a particular style convention (like using self) are not
always the most readable programs.

What is required is a convention that allows shorter notions when necessary
in a disciplined way.  I find that switching from self to _ is quite
satisfactory for this purpose under the right circumstances.  And, as such,
there is no need to extend Python's syntax for this purpose, which was the
initial topic of this thread.

Huaiyu

>
>-----Original Message-----
>From: Huaiyu Zhu [mailto:huaiyu at gauss.almadan.ibm.com]
>Sent: Wednesday, July 10, 2002 10:01 AM
>To: python-list at python.org
>Subject: Re: A better self
>
>
>Sean 'Shaleh' Perry <shalehperry at attbi.com> wrote:
>>> 
>>> Before you all react (programmers can be SO conservative), please
>think
>>> about it!
>>> 
>>
>>I have played with ideas like this in python and other languages, and
>witnessed
>>others do the same.
>>
>>.foo is REALLY hard to see if the font is not just right.
>>
>>.size = .r + .l
>>
>>is even worse.
>>
>>I am not 100% against the idea as an idea, but the actual usage is not
>all that
>>fun.
>
>A better (and currently available) convention is
>
>_.size = _.r + _.l
>
>It has been mentioned several times in this thread.  I've used it
>several
>times in the past and liked it, esp in iterative algorithms.  Some usage
>patterns include
>    
>    def __init__(_, x, y, z):
>        _.x, _.y, _.z = x, y, z
>
>    def process(_, x, y, z):
>        actions involving _.x, _.y, _.z, x, y, z
>        _.x, _.y, _.z = x, y, z
>
>    def update(_):
>        x, y, z = _.x, _.y, _.z 
>        actions involving x, y, z
>        _.x, _.y, _.z = x, y, z
>    
>One could even think of _. as a magic persistence prefix that makes the
>variable keep its value between method calls.  It is also easy to
>replace
>_.x with self.x or vice versa in a good editor.
>
>Huaiyu
>-- 
>http://mail.python.org/mailman/listinfo/python-list
>
>



More information about the Python-list mailing list