Python Forum
passing a string to a function to be a f-string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
passing a string to a function to be a f-string
#1
i don't know how easy or hard this would be to do. i want to have a function that gets a sting from a caller and make it be like an f-string and do it it the context of the caller even though the caller does not have it as an f-string literal. it might be read from a file, for example. the str can have "{n}" in it and the n i gets is the caller's variable n. anyone know how to do this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
The following worked for me
n = 48

def func(fstring):
    n = 100
    return eval('f' + repr(fstring))

if __name__ == '__main__':
    result = func("There are {n} items")
    print(result)
Output:
There are 100 items
Reply
#3
i want the caller's context. so, 48 would be expected.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
It is very difficult because I'm typing with my cat on my knees but this works
import sys

n = 48

def eval_fstring(fstring, globals=None, locals=None):
    """Evaluate an fstring
    
    arguments:
        fstring: the fstring to evaluate
        globals, locals: same meaning as in the eval() function

    returns:
        the string evaluated
    """
    f = sys._getframe(1)
    try:
        if globals is None:
            globals = f.f_globals
        elif locals is None:
            locals = globals
        if locals is None:
            locals = f.f_locals
        return eval('f' + repr(fstring), globals, locals)
    finally:
        del f, globals, locals

def foo():
    result = eval_fstring("There are {n} items")
    print(result)

def bar():
    n = 300
    result = eval_fstring("There are {n} items")
    print(result)
    

if __name__ == '__main__':
    foo()
    bar()
Output:
There are 48 items There are 300 items
Skaperen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  the quest for a mutable string (buffer) Skaperen 2 266 Apr-04-2024, 12:42 PM
Last Post: Skaperen
  the quest for a mutable string (more) Skaperen 0 174 Apr-04-2024, 04:01 AM
Last Post: Skaperen
  conversion of newlines in a string Skaperen 3 606 Jan-16-2024, 03:29 AM
Last Post: Skaperen
  run a string or str as a statement Skaperen 3 1,129 Sep-12-2023, 03:38 AM
Last Post: Gribouillis
  variables in f-string Skaperen 3 1,035 Jun-17-2023, 12:08 AM
Last Post: Skaperen
  code in a string? Skaperen 2 1,085 Nov-11-2022, 05:48 PM
Last Post: Skaperen
  other string types to consider? Skaperen 0 1,051 Oct-11-2022, 11:21 PM
Last Post: Skaperen
  continuing an f-string Skaperen 2 1,615 Oct-08-2021, 07:25 PM
Last Post: Skaperen
  passing type choicr to function Skaperen 2 1,815 Aug-22-2021, 12:01 AM
Last Post: Skaperen
  convert a string to a number Naheed 7 4,485 Apr-26-2021, 08:08 AM
Last Post: Naheed

Forum Jump:

User Panel Messages

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