[Tutor] drag-and-drop argument passing

Jeff Shannon jeff@ccvcorp.com
Mon, 01 Jul 2002 10:39:29 -0700

Pijus Virketis wrote:

> But how do I turn a string in a list, like sys.argv[], into a raw
> string? Is there a conversion function between the two or
> something?Then, I could do (pseudocode):for path in sys.argv[1:]:
> make_raw(path)

No, there isn't a conversion, because you don't need one.  The
difference between raw and not-raw strings exists only for string
literals that appear in your code -- it's a question of how you spell
a given sequence of bytes, not a difference in how those bytes are
stored, if that makes any sense.  In other words,

>>> x = "c:\\windows\\temp"
>>> y = r"c:\windows\temp"

These two strings, one raw and the other not, are the exact same
sequence of bytes.  In fact, if you "intern" the first string, then
the second one could well be the exact same object in memory.

Keep in mind that raw strings exist in order to avoid interpreting
control characters.  Control characters, in turn, are
multiple-character representations of a single, nonprintable
character.  Since I can't just type a tab character, for instance,
into my string, I use '\t' to represent that character instead, and
the interpreter will store that character sequence as a single byte
(0x09).  But what happens if I really want the two characters instead
of 0x09?  That's where raw strings come in -- by declaring a string as
raw, I'm telling the interpreter to *not* substitute any values, even
if a sequence *looks* like a control character sequence.

When you get strings from some source of input (whether it be
raw_input(), reading from a file, or from sys.argv), the strings are
already in internal representation -- that means that the distinction
between raw and not-raw strings is meaningless.  Any unprintable
characters will be passed in in their true form (0x09, for instance),
so there's no need to interpret any character sequences as anything

Hope that this makes sense -- if not, I'll try to explain a bit
better.  :)

In other words, your make_raw() function could be written like this --

def make_raw(text):
    return text

... and you'll get exactly the results you want.  ;)

Jeff Shannon
Credit International