1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Dynamically change one of f-string variables

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 16:33.

  1. Stack

    Stack Membro Participativo

    Is there a way to dynamically change/substitute one of f-string variables? Let's say I have format as follows:

    expected = f'{var} {self.some_evaluation_1(var)} {self.some_evaluation_2(var)}'


    Then I have test case with multiple assertions:

    var1='some value 1'
    var2='some value 2'
    var3='some value 3'

    var=var1
    self.assertEqual('some great value1', expected)
    var=var2
    self.assertEqual('some great value2', expected)
    var=var3
    self.assertEqual('some great value3', expected)



    Is there a way to make f-string use my variable instead of defined in initial format?

    # Let's say something like this? It's just a concept I know it doesn't work.
    self.assertEqual('some great value1', expected % var1)

    Continue reading...

Compartilhe esta Página