Unexpected string behaviour: txt = 'this' ' works'

C

c d saunter

I did a double take when debugging an error the other day. My
problem was missing out a comma when building a list of strings.

Much to my surprise the offending code still executed to cause
problems later on:
('this', 'works')
# As expectedthisworks
# Eh?

I have never seen this behaviour before, but it works in Python 2.2.1
and 2.5.4 so I guess it's meant to be there. I assume it is a feature
of the compiler.

Any thoughts?
Regards
Chris
 
B

Bruno Desthuilliers

c d saunter a écrit :
I did a double take when debugging an error the other day. My
problem was missing out a comma when building a list of strings.

Much to my surprise the offending code still executed to cause
problems later on:

('this', 'works')
# As expected
thisworks
# Eh?

I have never seen this behaviour before, but it works in Python 2.2.1
and 2.5.4 so I guess it's meant to be there. I assume it is a feature
of the compiler.

Any thoughts?

http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation
 
C

c d saunter

Bruno Desthuilliers ([email protected]) wrote:
: c d saunter a écrit :
: > I did a double take when debugging an error the other day. My
: > problem was missing out a comma when building a list of strings.
: >
: > Much to my surprise the offending code still executed to cause
: > problems later on:
: >
: >>>> txt = 'this', 'works'
: >>>> print txt
: > ('this', 'works')
: > # As expected
: >>>> txt = 'this' 'works'
: >>>> print txt
: > thisworks
: > # Eh?
: >
: > I have never seen this behaviour before, but it works in Python 2.2.1
: > and 2.5.4 so I guess it's meant to be there. I assume it is a feature
: > of the compiler.
: >
: > Any thoughts?

: http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation

Ahh. Thanks.

Chris
 
B

bearophileHUGS

(e-mail address removed) (c d saunter):
I assume it is a feature of the compiler.<

Yes it's a bug-prone antifeature that's probably a relic from C that
doesn't have a concatenation operator among strings as Python does
(+). So in Python it saves you to use + at the cost of possible bugs.

Bye,
bearophile
 
J

Jason

(e-mail address removed) (c d saunter):


Yes it's a bug-prone antifeature that's probably a relic from C that
doesn't have a concatenation operator among strings as Python does
(+). So in Python it saves you to use + at the cost of possible bugs.

Bye,
bearophile

I've used this feature in C and Python when I want to wrap strings
without having a newlines in the strings... grouping the whole bunch
with a set of parenthesis to make the concatenation explicit.
Admittedly, I wouldn't be broken up if this feature became deprecated,
as

It does do a minor optimization in Python and most C compilers. The
two string constants are concatenated when the code is parsed, rather
than when the code is executed.
.... return 'This is ' 'an example.'
.... 2 0 LOAD_CONST 1 ('This is an example.')
3 RETURN_VALUE
It's such a minor optimization, that you probably wouldn't see any
effect on your program.

--Jason
 
B

bearophileHUGS

Jason:
It's such a minor optimization, that you probably wouldn't see any
effect on your program.
.... return 'This is ' + 'an example.'
.... 2 0 LOAD_CONST 3 ('This is an example.')
3 RETURN_VALUE

Bye,
bearophile
 
S

Steven D'Aprano

Jason:
2 0 LOAD_CONST 3 ('This is an example.')
3 RETURN_VALUE


That's a feature of the CPython keyhole optimizer, not a language
feature. I expect that if you try that same thing in various other
Pythons (and earlier versions of CPython) you'll get a different result.
And like all optimizations, it's not a language feature, it's subject to
removal without notice if necessary.

If you want guaranteed implicit concatenation, you need to leave out the
plus operator. That is a language feature.

And I do call it a feature. I find it useful, and I've never run into any
bugs caused by it, and if I did, I expect they would show up quickly:

x = "foo", "bar"
y = "foo" "bar"

x is a tuple of length 2, y is a string of length 6. You'll soon notice
the difference. Since it only effects literals, it should be easy enough
to find.
 

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
474,298
Messages
2,571,542
Members
48,282
Latest member
PrincessX3

Latest Threads

Top