[docs] Bug?

Sandro Tosi sandro.tosi at gmail.com
Sun Feb 26 14:04:33 CET 2012


Hello Peter,

2012/2/24 Peter Demchenko <pdemcenko at hotmail.com>:
>
> Hi,
> May be I miss something, but look...
> Why the script:
> ----------------
> x="ab"
> 'cde'
> print(len(x))
> ----------------
>
>  - responds with 2?

what else would you expect? 5 maybe? I'll try to explain below why 2 is correct

> Whitespaces after "ab" can be or no - result is the same: 2

do you mean "ab " or something else?

> It's rather dangerous situation, since the next line 'cde' produces no
> compilation errors

thecnically speaking, python is not compiled, just interpreted and a
bytecode version is generated for speed-up reasons. Also it's a valid
expression, a string, and it doesn't generate any warning/error. if
you typed it on an interactive prompt, it will just print back the
string.

> so you'll get string x stripped!

it's not stripped: you decided to terminate the x definition when you
press ENTER after "ab" so 'cde' is another line, another command,
*not* related to x

> !!  An excerpt from manual:
>  "2.4.2. String literal concatenation
> Multiple adjacent string literals (delimited by whitespace), possibly using
> different quoting conventions, are allowed, and their meaning is the same as
> their concatenation. Thus, "hello" 'world' is equivalent to "helloworld".
> This feature can be used to reduce the number of backslashes needed, to
> split long strings conveniently across long lines, ..."
>
> Ok. Now look at another excerpt from the same man:
> !!  "string.whitespace
> A string containing all characters that are considered whitespace. On most
> systems this includes the characters space, tab, linefeed, return, formfeed,
> and vertical tab"

yes but it's also clear in the lexical description what a shortstring
is and that to use the literals concatenation the strings need to be
at the same syntax level.

To cut a long story short, it's simply not working as you expect :)
Either you use a multi-line string literal:

"""ab
cde"""

or a line continuation '\' and so on.

Regards,
-- 
Sandro Tosi (aka morph, morpheus, matrixhasu)
My website: http://matrixhasu.altervista.org/
Me at Debian: http://wiki.debian.org/SandroTosi


More information about the docs mailing list