[Python-ideas] Draft PEP on string interpolation

Eric V. Smith eric at trueblade.com
Wed Aug 26 17:39:24 CEST 2015


On 08/26/2015 11:06 AM, Eric V. Smith wrote:
> On 08/26/2015 10:51 AM, Ron Adam wrote:

>> I don't have time to test yours this morning, but What happens in this
>> case?
>>
>>     x = [1]
>>     ix = i('{x}')
>>     x = [2]          # Mutates i-string content?
>>     print(str(ix))
>>
>> Does this print "[1]" or "[2]"?
> 
> I added a similar test this morning. My code produces "[2]". I can't
> imagine a design that could produce a different result, but follow the
> "delayed evaluation of the string" model.

Oops, I misread this as mutating x. Mine would produce "[1]". Here are
the tests:

            # changing a mutable value doesn't affect the i-string
            n = 0
            x = i('{n}')
            self.assertEqual(str(x), '0')
            n = 1
            self.assertEqual(str(x), '0')

            # but a mutable value will
            l = [1]
            x = i('{l}')
            self.assertEqual(str(x), '[1]')
            l[0] = 2
            self.assertEqual(str(x), '[2]')
            l = [3]
            self.assertEqual(str(x), '[2]')

Eric.



More information about the Python-ideas mailing list