raw strings and \

P

plahey

I thought I understood raw strings, then I got bitten by this:

x=r'c:\blah\'

which is illegal! I thought that \ had no special meanning in a raw
string so why can't it be the last character?

making me do:

x=r'c:\blah' '\\'

is just silly...
 
D

Duncan Booth

wrote:
I thought I understood raw strings, then I got bitten by this:

x=r'c:\blah\'

which is illegal! I thought that \ had no special meanning in a raw
string so why can't it be the last character?

No, the backslash is still special in terms of parsing the string, it
is simply that once the string has been parsed escape sequences are not
interpreted.
making me do:

x=r'c:\blah' '\\'

is just silly...
Yes, that is silly. You could alternatively do:

x = 'c:/blah/'

or

x = 'c:\\blah\\'

But why would you want to end your path with a literal backslash anyway?
Get into the habit of using the os.path module for all manipulations of
file paths and you never need to worry about this sort of thing again.

This sort of thing should work well enough:

folder = 'c:/blah'

for line in open(os.path.join(folder, 'data.txt')):
print line
 
P

plahey

Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.
 
A

Alex Martelli

Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.

The alternative would have been to offer no way at all for a rawstring
to include its quoting-character. Since rawstrings were designed to
support regular expressions (which never need to end with a backslash),
that was considered a higher cost -- there is no real advantage to
supporting backslash-using Dos/Windows path literals, even though many
Windowsers use rawstrings for those.


Alex
 
B

Blackbird

Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.

But at the parsing stage, it *is* somewhat special, even if it is not the
last charater. E.g,

a = r'check \' this'
print a

The second quote is not regarded as ending the string literal.
 
S

Steven D'Aprano

Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.

I don't deny that this is a gotcha, but you misunderstand the purpose of
raw strings. They weren't designed so that Windows users could enter
pathnames with backslashes. Raw strings are designed to enter regular
expressions, and for regular expressions, not being able to end a string
with a backslash is not a bug but a feature.

See http://www.ferg.org/projects/python_gotchas.html#contents_item_2

(and try not to choke on the oh-so-saccharine-sweet cutesy introduction.
Red Ridinghood indeed *wink*)
 
P

plahey

Hi Alex,

thanks for the reply. I can see that there is a choice that needed to
be made. Before I was aware of the \ issue I just used (yes it has
come up, but the example is at work) triple quotes to work around the
embedded quote issue:

x=r'''It's like this "c:\blah\" ok?'''
print x
It's like this "c:\blah\" ok?

Am I missing something?

I guess my point is that it is quite surprising that r'\' is not a
legal way to represent a backslash. I look for consistency in the
tools that I work with and this except-for-the-last-character exception
is annoying (I admit it might be a silly use-case but consistency is
consistency).

Again, thanks for taking the time to reply.

Now get back to work on your new Nutshell book :)
 
P

plahey

Hi,

thanks for the reply. I was not aware of this in raw strings (and
frankly, don't like it... but who cares about that :) )

When I needed embedded quotes in a raw string I went the triple quote
route:

a = r'''check \' this'''

which makes more sense to me.
 
P

plahey

Hi Steven,

thanks for the reply. I was/am aware that raw strings are mainly used
for regular expressions (and franky that was I usually use them for).
I was not aware that they still have "special powers" in raw strings
(thanks for the link!).

This one bit me when I was doing some quick and dirty hacking and a 5
minute task ended up being a much longer adventure becuase of this
(Note to self: don't post when annoyed).
 
A

Alex Martelli

thanks for the reply. I can see that there is a choice that needed to
be made. Before I was aware of the \ issue I just used (yes it has
come up, but the example is at work) triple quotes to work around the
embedded quote issue:

x=r'''It's like this "c:\blah\" ok?'''
print x
It's like this "c:\blah\" ok?

Am I missing something?

No, triplequotes are perfectly valid.
I guess my point is that it is quite surprising that r'\' is not a
legal way to represent a backslash. I look for consistency in the
tools that I work with and this except-for-the-last-character exception
is annoying (I admit it might be a silly use-case but consistency is
consistency).

Alas, somebody will now quote Emerson at you, I fear;-). Me, I
appreciate consistency and regularity more than most (without making a
fetish of it), but the rule "a rawstring cannot end with an odd number
of backslashes" is perfectly regular and consistent, so I'm not
perturbed by it (it's NOT about the last character, as r'\\' is just
fine even though its last character is a \ -- it's about the parity of
the length of the terminating sequence of backslashes: even is OK [0 is
of course even;-)] and odd is not).
Again, thanks for taking the time to reply.

Now get back to work on your new Nutshell book :)

Yep, good point!-)


Alex
 
S

Steve Holden

Hi,

thanks for the reply. I was not aware of this in raw strings (and
frankly, don't like it... but who cares about that :) )
Healthy attitude!
When I needed embedded quotes in a raw string I went the triple quote
route:

a = r'''check \' this'''

which makes more sense to me.
But remember you could equally well have used

a = "check \\' this"

I presume you do *want* that backslash in there? If not, of course, then

a = "check ' this"

is by far the simplest way to represent what you want. because Python
offers four different string quotes, each of which can be raw, there's
always a way ...

regards
Steve
 
P

plahey

Alas, somebody will now quote Emerson at you, I fear;-).

Let them come :)

I almost always see this (mis)quoted as:

"consistency is the hobgoblin of little minds"

which is not really what Emerson said. The full quote is:

"A foolish consistency is the hobgoblin of little minds"

I would say that a foolish anything is something to avoid. I don't
think that anyone would claim that inconsistency is a virtue in a
computer language (or in anything else for that matter).
 
B

Blackbird

Slightly OT, but here is a crazy little program that shows the power of
using raw strings:

s=r'print "s=r\'%s\'\n%s"%(s,s)'
print "s=r\'%s\'\n%s"%(s,s)

When run, this program will print an exact copy of itself.

Blackbird
 
S

Steven D'Aprano

Almost off-topic.

Well, okay, completely off-topic.

I would say that a foolish anything is something to avoid. I don't
think that anyone would claim that inconsistency is a virtue in a
computer language (or in anything else for that matter).

Inconsistency can be a virtue whenever predictability is a vice.

Imagine, for example, that Fred and Bill are negotiating over something
(say, Fred wants to buy Bill's house). If Fred is predictable, then Bill
can work out just how high Fred is willing to pay, and refuse to accept a
penny less. In other words, if you are too consistent, people will learn
to take advantage of that consistency to get the most from you for the
least in return.

On the other hand, if Fred is unpredictable -- that is, inconsistent -- it
is much harder to predict his behaviour, and Bill will be more cautious
and more likely to settle for a lower offer.

Think about hostile negotiations. If people know exactly how far they
can push you before you will get mad, they will push right to the edge.
But if they don't know where that edge lies, if they are uncertain how you
will react ("if I demand the Sudetenland, will he break off negotiations
and launch a preemptive attack?"), they will be more cautious, less
demanding, more open to compromise.

Cognitive scientists believe that the benefits of unpredictability and
inconsistency are behind the evolution of such irrational emotions as
jealousy and rage. In that sense, their very irrationality is what makes
them rational.
 
F

Fredrik Lundh

Blackbird said:
Slightly OT, but here is a crazy little program that shows the power of
using raw strings:

s=r'print "s=r\'%s\'\n%s"%(s,s)'
print "s=r\'%s\'\n%s"%(s,s)

When run, this program will print an exact copy of itself.

I'm not sure what the raw strings brings to the table, though; it's not like you need
them to write a "self-replicating" python program:

http://miscoranda.com/37

</F>
 
B

Blackbird

Fredrik said:
I'm not sure what the raw strings brings to the table, though; it's
not like you need them to write a "self-replicating" python program:

http://miscoranda.com/37


Great link! I wasn't aware that this was called a Quine. This one is
great:

_='_=%r;print _%%_';print _%_

When I hacked down the two lines, I didnt see that assigning the variable to
the beginning of the program, instead of the end, essentially gets rid of
literals in the actual print statement. And now I also learned about %r.
 
F

Fredrik Lundh

I guess my point is that it is quite surprising that r'\' is not a
legal way to represent a backslash. I look for consistency in the
tools that I work with and this except-for-the-last-character exception
is annoying

you seem to be missing that "exception" you think you're seeing is
the consequence of a consistency: *all* literal strings are parsed
in the same way, no matter what's prefix qualifier they're using.

</F>
 
J

John Salerno

Alex said:
Yep, good point!-)

Are you working on a new edition? I didn't see any new Python books
listed on O'Reilly's site through April, but I'd definitely be
interested in new versions of the books I plan to get soon (Cookbook and
Nutshell, for example, although I figure the Cookbook is fairly new at
this point.)
 
P

plahey

Interesting, so thats where this comes from.

Of course the problem is that I thought that a raw string was a
different animal than a regular string (i.e. truely "raw" where no
characters are special, except of course the closing quote) so parsing
consistency is surprising to an end user (who has an overly-simplistic
mental model).

I'm sure that this choice greatly simplifies the internals of Python.
As an end user, of course, I couldn't care less about this (just as my
end users don't care about my problems :) ). I would still prefer my
still-bloody raw strings, but I can live with the slightly-cooked ones
now that I've got it...

Thanks!
 
A

Alex Martelli

John Salerno said:
Are you working on a new edition? I didn't see any new Python books
listed on O'Reilly's site through April, but I'd definitely be
interested in new versions of the books I plan to get soon (Cookbook and
Nutshell, for example, although I figure the Cookbook is fairly new at
this point.)

Yep, a 3rd edition of the Cookbook is not in the cards. A 2nd edition
of the Nutshell is, and if all goes well should be out before the fall.


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top