Arvind Narayanan's journal

In my last post I asked why the expression >>>''r''=="" evalutes to True in python. As I expected, the responses were a mix of "duh," bewilderment, and partial answers :-)

Ok, so here's what's going on. The first thing to notice is that I have two pairs of single quotes on the left and a pair of double quotes on the right. With some browser fonts, the two look almost identical, whereas with others the difference is obvious. One way to see the difference clearly is to copy-paste the text into something that uses a fixed-width font, such as the box where you type in comments :-)

The next thing to note is that the expression ''r'' is tokenized as '' followed by r'', and the two tokens are concatenated—python has implicit string concatenation, somewhat like bash (except that in python you can have spaces between the strings, and bash doesn't distinguish between strings and commands, which Tcl takes to an extreme.) Try this in bash sometime: $ "l"''"s"

But what's r''? That's a python "raw string," which means that backslashes don't get interpreted, although that doesn't change anything here.

The other things to know about python strings are that triple quotes are also valid (they are multi-line quotes), and that you can multiply strings. You can probably guess what this evaluates to: >>>"ha "*3

You can do insane things combining by combining single, double, and triple quotes with raw strings. Here's something I came up with in 5 minutes, I'm sure you can do significantly more horrifying things if you put your mind to it. Do any two of these evaluate to the same thing? (Try it out!)

Significant whitespace

Is anything other than the *leading* whitespace significant in Python? I thought only the whitespace at the beginning of the line matters, and that too only relatively (whether a particular line has more leading whitespace or less than the previous line): http://www.secnetix.de/~olli/Python/block_indentation.hawk

Re: Significant whitespace

that's correct. but it's still generally called significant whitespace.

people who argue against the use are generally people who are trying to fight the negative knee-jerk reaction that whitespace indentation gets from ignorant programmers. since i love whitespace indentation, i don't really care.