?

Log in

No account? Create an account
Python puzzle solution - Arvind Narayanan's journal [entries|archive|friends|userinfo]

Python puzzle solution [Jan. 8th, 2009|11:37 pm]
Arvind Narayanan
[Tags|, ]

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!)
>>>""'''''''''''r''''''''''', ''""''''''''r'''''''''''', ''""'''''''''r''''''''''', ''''''""'''''r''''''''''', ''''''''""'''r''''''''''', '''''''''''""r'''''''''''
Together with the signficant whitespace that python is notorious for, no serious language has made you work so hard to understand it's lexer!
LinkReply

Comments:
[User Picture]From: hukuma
2009-01-09 09:32 pm (UTC)
I think using a proportional font in your puzzle was cheating. :)
(Reply) (Thread)
[User Picture]From: arvindn
2009-01-09 10:10 pm (UTC)
oh c'mon, that was the whole puzzle!
(Reply) (Parent) (Thread)
From: (Anonymous)
2009-01-10 10:37 pm (UTC)

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
(Reply) (Thread)
[User Picture]From: arvindn
2009-01-10 10:47 pm (UTC)

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.
(Reply) (Parent) (Thread)