Why don't f-strings change when variables they reference change?

怎甘沉沦 提交于 2020-08-19 06:17:06

问题


While playing with new f-strings in the recent Python 3.6 release, I've noticed the following:

  1. We create a foo variable with value bar:

    >>> foo = 'bar'
    
  2. Then, we declare a new variable, which is our f-string, and it should take foo to be formatted:

    >>> baz = f'Hanging on in {foo}'
    
  3. Ok, all going fine and then we call baz to check its value:

    >>> baz
    'Hanging on in bar'
    
  4. Let's try to change the value of foo and call baz again:

    >>> foo = 'spam'
    >>> baz
    'Hanging on in bar'
    

Shouldn't it be dynamic? Why does this happen? I thought the f-string would update if the value of foo changed, but this didn't happened. I don't understand how this works.


回答1:


The f-string has already been evaluated when you executed:

>>> baz = f'Hanging on in {foo}'

Specifically, it looked up the value for the name foo and replaced it with 'bar', the value that was found for it. baz then contains the string after it has been formatted.

f-strings aren't constant; meaning, they don't have a replacement field inside them waiting for evaluation after being evaluated. They evaluate when you execute them, after that, the assigned value is just a normal string:

>>> type(f'hanging on in {foo}')
<class 'str'>

For reference, see the section on Formatted String Literals:

[..] While other string literals always have a constant value, formatted strings are really expressions evaluated at run time. [..]

After the expression (the look-up for the replacement field and its consequent formatting) is performed, there's nothing special about them, the expression has been evaluated to a string and assigned to baz.




回答2:


Strings are immutable and once a string is created, it can no longer be changed.

foo and more importantly baz are both strings. That means when you create them they go into memory and can no longer be changed.

Once you assigned foo = bar you created this object and assigned it to a specific location in memory. Same thing was done with baz.

Even though baz was as a Format string literal does not mean that it is no longer immutable since:

In [4]: type(baz)
Out[4]: str

By doing so, baz was created as an object and assigned to your memory as Hanging on in bar, thus its relation to foo is purely during instantiation. During which baz seeks the object foo and concatenate it where appropriate.

Once you created foo = 'spam' you destroyed the original assignment of foo and create a new one in memory.



来源:https://stackoverflow.com/questions/41306928/why-dont-f-strings-change-when-variables-they-reference-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!