Author arcivanov
Recipients arcivanov
Date 2018-02-26.10:42:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1519641773.0.0.467229070634.issue32954@psf.upfronthosting.co.za>
In-reply-to
Content
As an example this is the current state of affairs:

>>> def x():
...     foo = "foo"
...     s1 = f"S1 value {foo}"
...     s2 = f"S2 value {s1}"
...     print(s2)
... 
>>> dis.dis(x)
  2           0 LOAD_CONST               1 ('foo')
              2 STORE_FAST               0 (foo)

  3           4 LOAD_CONST               2 ('S1 value ')
              6 LOAD_FAST                0 (foo)
              8 FORMAT_VALUE             0
             10 BUILD_STRING             2
             12 STORE_FAST               1 (s1)

  4          14 LOAD_CONST               3 ('S2 value ')
             16 LOAD_FAST                1 (s1)
             18 FORMAT_VALUE             0
             20 BUILD_STRING             2
             22 STORE_FAST               2 (s2)

  5          24 LOAD_GLOBAL              0 (print)
             26 LOAD_FAST                2 (s2)
             28 CALL_FUNCTION            1
             30 POP_TOP
             32 LOAD_CONST               0 (None)
             34 RETURN_VALUE

whereas an fl-string representation of:

>>> def x():
...     foo = "foo"
...     s1 = fl"S1 value {foo}"
...     s2 = fl"S2 value {s1}"
...     print(s2)
... 

would behave close to:

>>> def x():
...     foo = "foo"
...     s1 = lambda: f"S1 value {foo}"
...     s2 = lambda: f"S2 value {s1()}"
...     print(s2())
... 
>>> dis.dis(x)
  2           0 LOAD_CONST               1 ('foo')
              2 STORE_DEREF              0 (foo)

  3           4 LOAD_CLOSURE             0 (foo)
              6 BUILD_TUPLE              1
              8 LOAD_CONST               2 (<code object <lambda> at 0x7ff2ef1abd20, file "<stdin>", line 3>)
             10 LOAD_CONST               3 ('x.<locals>.<lambda>')
             12 MAKE_FUNCTION            8
             14 STORE_DEREF              1 (s1)

  4          16 LOAD_CLOSURE             1 (s1)
             18 BUILD_TUPLE              1
             20 LOAD_CONST               4 (<code object <lambda> at 0x7ff2ef1abae0, file "<stdin>", line 4>)
             22 LOAD_CONST               3 ('x.<locals>.<lambda>')
             24 MAKE_FUNCTION            8
             26 STORE_FAST               0 (s2)

  5          28 LOAD_GLOBAL              0 (print)
             30 LOAD_FAST                0 (s2)
             32 CALL_FUNCTION            0
             34 CALL_FUNCTION            1
             36 POP_TOP
             38 LOAD_CONST               0 (None)
             40 RETURN_VALUE

Where LOAD_CONST, LOAD_CONST, MAKE_FUNCTION sequences are replaced with BUILD_LAZY_STRING (name's provisional) producing the same lambda-like formatter function that cooperates with FORMAT_VALUE and BUILD_STRING/BUILD_LAZY_STRING ops.
History
Date User Action Args
2018-02-26 10:42:53arcivanovsetrecipients: + arcivanov
2018-02-26 10:42:53arcivanovsetmessageid: <1519641773.0.0.467229070634.issue32954@psf.upfronthosting.co.za>
2018-02-26 10:42:52arcivanovlinkissue32954 messages
2018-02-26 10:42:52arcivanovcreate