FAQ
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?
Regards
Chris

Search Discussions

  • Quentin Gallet-Gilles at Feb 11, 2009 at 11:21 am
    *Literal* string concatenation has always been a part of Python :
    http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation

    On Wed, Feb 11, 2009 at 12:06 PM, c d saunter wrote:

    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?
    Regards
    Chris



    --
    http://mail.python.org/mailman/listinfo/python-list
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20090211/6d6e5db5/attachment.htm>
  • Bruno Desthuilliers at Feb 11, 2009 at 11:23 am

    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
  • C d saunter at Feb 11, 2009 at 11:26 am
    Bruno Desthuilliers (bruno.42.desthuilliers at websiteburo.invalid) 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
  • BearophileHUGS at Feb 11, 2009 at 12:16 pm
    christopher.saun... at durham.ac.uk (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
  • Jason at Feb 11, 2009 at 8:09 pm

    On Feb 11, 5:16?am, bearophileH... at lycos.com wrote:
    christopher.saun... at durham.ac.uk (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
    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.
    from dis import dis
    def f():
    ... return 'This is ' 'an example.'
    ...
    dis(f)
    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
  • BearophileHUGS at Feb 11, 2009 at 8:57 pm

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

    Bye,
    bearophile
  • Steven D'Aprano at Feb 12, 2009 at 12:57 am

    On Wed, 11 Feb 2009 12:57:31 -0800, bearophileHUGS wrote:

    Jason:
    It's such a minor optimization, that you probably wouldn't see any
    effect on your program.
    from dis import dis
    def f():
    ... return 'This is ' + 'an example.' ...
    dis(f)
    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.




    --
    Steven

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 11, '09 at 11:06a
activeFeb 12, '09 at 12:57a
posts8
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase