Escapeism

Steve Holden steve at holdenweb.com
Sat Sep 30 17:52:48 EDT 2006


Kay Schluehr wrote:
> Steve Holden wrote:
> 
>>Kay Schluehr wrote:
>>
>>>Sybren Stuvel wrote:
>>>
>>>
>>>>Kay Schluehr enlightened us with:
>>>>
>>>>
>>>>>Usually I struggle a short while with \ and either succeed or give up.
>>>>>Today I'm in a different mood and don't give up. So here is my
>>>>>question:
>>>>>
>>>>>You have an unknown character string c such as '\n' , '\a' , '\7' etc.
>>>>>
>>>>>How do you echo them using print?
>>>>>
>>>>>print_str( c ) prints representation '\a' to stdout for c = '\a'
>>>>>print_str( c ) prints representation '\n' for c = '\n'
>>>>>...
>>>>>
>>>>>It is required that not a beep or a linebreak shall be printed.
>>>>
>>>>try "print repr(c)".
>>>
>>>
>>>This yields the hexadecimal representation of the ASCII character and
>>>does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
>>>escape semantics. One way to achieve this naturally is by prefixing
>>>'\a' with r where r'\a' indicates a "raw" string. But unfortunately
>>>"rawrification" applies only to string literals and not to string
>>>objects ( such as c ). I consider creating a table consisting of pairs
>>>{'\0': r'\0','\1': r'\1',...}  i.e. a handcrafted mapping but maybe
>>>I've overlooked some simple function or trick that does the same for
>>>me.
>>>
>>
>>No, you've overlooked the fact that if you print the string containing
>>the two characters "backslash" and "lower case a" then it will print
>>exactly those two characters. See:
>>
>>In [30]: c = "\\a"
>>
>>In [31]: len(c)
>>Out[31]: 2
>>
>>In [32]: print c
>>\a
> 
> 
> I'm interested in the transition between two literals from which one is
> a string literal containing \ as a "meta character" s.t. '\a' has
> actually length 1 and is beep when printed to stdout and its "raw" form
> without a meta character interpretation of \ that leads to the result
> you described. Using the string prefix r to '\a' indicates the raw form
> to the compiler. But there seems to be no runtime counterpart. I've
> suggested a naive implementation such as
> 
> def rawform(c):
>      return {'\a': r'\a'}[c]
> 
> Here the function returns for the single input character '\a' the two
> character raw form by means of escaping \ ( and raises a KeyError
> exception otherwise ).
> 
> 
>>>>c = '\a'
>>>>print rawform(c)
> 
> \a
> 
> This has the same effect as writing:
> 
>>>>c = r'\a'
>>>>print c
> 
> \a
> 
> But there is some ambiguity due to the the fact that applying '\7' to
> rawform() yields r'\a' and not r'\7'. So one needs more specification
> for disambiguation using e.g. an extra parameter.
> 
In [33]: "\7" == "\a"
Out[33]: True

Sorry. It can't possibly know which of two alternative representation 
were used to represent a particular character in a literal.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list