Too much code - slicing

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 19 01:25:48 EDT 2010


On Sun, 19 Sep 2010 00:35:14 +0000, Seebs wrote:

> On 2010-09-19, AK <andrei.avk at gmail.com> wrote:
>> Funny that you should say that, because I thought quite a few times
>> that it would be really awesome if some texts in English had syntax
>> highlighting. Obviously, not Brothers Karamazov, but something like a
>> tutorial, or a manual, or an online article. If key words were
>> highlighted, I'd be able to quickly glance over parts that are not
>> useful to me at the time, and find the interesting bits.
> 
> That wouldn't be *syntax* highlighting, that'd be *semantic*
> highlighting.
> 
> Which people often do -- notice that I did it twice in that paragraph. 
> But that's the point -- you need to know what it *means* to make
> sensible decisions about what to highlight.  Syntax highlighting is
> precisely the opposite, highlighting things for reasons that have
> nothing to do with their semantic content.  It distracts from the actual
> meaning of the code.

I'm not entirely sure I agree with you here... you can't ignore syntax in 
order to understand the meaning of code. Unless you understand the 
syntax, even a simple line like:

x = y

is impossible to understand. Is it an assignment? If so, which way (does 
it assign the value of x to the variable y, or y to x)? Or is it a 
comparison?

The term "syntax highlighting" for what editors I've seen do is actually 
misleading -- they don't highlight *syntax*, they try to highlight 
*semantics*. When your editor highlights the function len() in the 
expression "x = len(y) + spam(z)" but not the function spam(), you know 
it has nothing to do with syntax. len() is singled out because of its 
semantics, namely the fact that it's a built-in.


In English, the meaning of the some sentences do benefit by syntax 
highlighting, and in fact that's partly what punctuation is for: English 
partly uses punctuation marks as tags to break the sentence structure 
into sub-sentences, clauses and terms (particularly when the sentence 
would otherwise be ambiguous).

An extreme example is the infamous "buffalo" sentence, which is 
grammatically valid and semantically meaningful but rather difficult to 
decipher:

http://en.wikipedia.org/wiki/Buffalo_buffalo_Buffalo_buffalo_buffalo_buffalo_Buffalo_buffalo


Of course this is an extreme example, but English is often ambiguous, and 
some sort of syntax highlighting could potentially be useful:

"Woman shoots man with crossbow"

Was it the man armed with a crossbow, or the woman? If we could somehow group the 
clause "with crossbow" with "woman" or "man" by something *other* than 
proximity, we could remove the ambiguity.

In ordinary English, not only does punctuation sometimes play the role of 
syntax highlighting, but so do otherwise meaningless words like "the", 
"a", "which", "that", "also", and so forth.

Bringing it back to Python, that's why punctuation like : are useful. 
They're syntax highlighting.


-- 
Steven



More information about the Python-list mailing list