[Python-Dev] Subtle difference between f-strings and str.format() (original) (raw)
Chris Jerdonek chris.jerdonek at gmail.com
Fri Mar 30 13:33:07 EDT 2018
- Previous message (by thread): [Python-Dev] Subtle difference between f-strings and str.format()
- Next message (by thread): [Python-Dev] Subtle difference between f-strings and str.format()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Fri, Mar 30, 2018 at 4:41 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
On 30 March 2018 at 21:16, Nathaniel Smith <njs at pobox.com> wrote:
And bool(obj) does always return True or False; if you define a bool method that returns something else then bool rejects it and raises TypeError. So bool(bool(obj)) is already indistinguishable from bool(obj).
However, the naive implementation of 'if a and True:' doesn't call bool(bool(a)), it calls bool(a) twice, and this is distinguishable by user code, at least in principle. For example: >>> class FlipFlop: ... state = False ... def bool(self): ... result = self.state ... self.state = not result ... return result ... >>> toggle = FlipFlop() >>> bool(toggle) False >>> bool(toggle) True >>> bool(toggle) False >>> bool(toggle) and bool(toggle) False >>> toggle and toggle <_main_.FlipFlop object at 0x7f35293604e0> >>> bool(toggle and toggle) True So the general principle is that bool implementations shouldn't do anything that will change the result of the next call to bool, or else weirdness is going to result.
I don't think this way of stating it is general enough. For example, you could have a nondeterministic implementation of bool that doesn't itself carry any state (e.g. flipping the result with some probability), but the next call could nevertheless still return a different result. So I think Nathaniel's way of stating it is probably better:
If we want to change the language spec, I guess it would be with text like: "if bool(obj) would be called twice in immediate succession, with no other code in between, then the interpreter may assume that both calls would return the same value and elide one of them".
--Chris
- Previous message (by thread): [Python-Dev] Subtle difference between f-strings and str.format()
- Next message (by thread): [Python-Dev] Subtle difference between f-strings and str.format()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]