Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f-string-ify
#3
i can't share the real code, but i can make an example. don't make other assumptions about this example. don't try to "solve" it some other way (the "solution" is probably not applicable to other cases)
if case1:
    ...
    foo,bar = ...
    ...
    print(f'foo = {foo!r}  and bar = {bar!r}')
    ...
if case2:
    ...
    foo,bar = ...
    ...
    print(f'foo = {foo!r}  and bar = {bar!r}')
    ...
if case3:
    ...
    foo,bar = ...
    ...
    print(f'foo = {foo!r}  and bar = {bar!r}')
    ...
what i want is something more like:
...
pattern = 'foo = {foo!r}  and bar = {bar!r}'
...
if case1:
    ...
    foo,bar = ...
    ...
    print(fy(pattern))
    ...
if case2:
    ...
    foo,bar = ...
    ...
    print(fy(pattern))
    ...
if case3:
    ...
    foo,bar = ...
    ...
    print(fy(pattern))
    ...
where fy() in this example does the f-string-ify work. there will likely be a suggestion to have a function with the f-string coded once in the function and to call that function in each case, passing the variables to it. but that assumes a trivial set of variables. maybe there are too many. maybe they are unknown. maybe i'd like to read the pattern from a config file.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
f-string-ify - by Skaperen - Jan-19-2020, 06:13 AM
RE: f-string-ify - by gruntfutuk - Jan-20-2020, 03:45 PM
RE: f-string-ify - by Skaperen - Jan-20-2020, 11:48 PM
RE: f-string-ify - by gruntfutuk - Jan-22-2020, 05:44 PM
RE: f-string-ify - by Skaperen - Jan-24-2020, 05:54 AM
RE: f-string-ify - by gruntfutuk - Jan-24-2020, 06:08 PM
RE: f-string-ify - by Skaperen - Jan-24-2020, 09:15 PM
RE: f-string-ify - by gruntfutuk - Jan-25-2020, 12:03 PM
RE: f-string-ify - by Skaperen - Jan-25-2020, 07:28 PM
RE: f-string-ify - by gruntfutuk - Jan-26-2020, 10:54 AM
RE: f-string-ify - by Skaperen - Jan-27-2020, 01:33 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020