0
  • below code is my valid requirement.
import numpy as np
a =np.array([1, 3, 0, 2], int)
b =np.array([5, 2, 1, 2], int)
print(f'{a > b = }')
  • result is ok
a > b = array([False,  True, False, False])

  • I wanna make inner function which have f-string style.
import numpy as np
a =np.array([1, 3, 0, 2], int)
b =np.array([5, 2, 1, 2], int)

def myprn(text):
    print(f'{text = }')

myprn(a > b)
  • result is not my requirement
  • I wanna print 'a > b' instead of 'text'
text = array([False,  True, False, False])
#^^^ how to fix this requirement?
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
dEitY719
  • 1
  • 1
  • Your f-string syntax is wrong. Why do you end your curly brackets with `=`? – Simon Lundberg Jan 09 '23 at 12:07
  • 2
    @SimonLundberg this is Python >3.8 – cheersmate Jan 09 '23 at 12:08
  • 2
    This won't work because `a > b` is evaluated and saved as `text` before it is used in the f-string. What to do really depends on your use case. As is, your function does nothing, so you might as well use the print line directly. If the function actually is more complex, you could add a second argument where you pass a descriptive string and print that. – cheersmate Jan 09 '23 at 12:08
  • The only way I can think to make something similar to this work is `print(f'{text} = {eval(text)}')` and then passing the function strings with the expression, i.e. `myprn('a > b')` – Tomerikoo Jan 09 '23 at 12:09
  • @SimonLundberg This is valid syntax. See https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging – Tomerikoo Jan 09 '23 at 12:10
  • Oh, huh, I didn’t know about that. Been stuck in 3.7 for too long. – Simon Lundberg Jan 09 '23 at 12:13
  • @Tomerikoo Thank you for your tip. I have knew syntax eval() function. Your answer is fine and my requirement. I checked valid operation. Thank you very much. – dEitY719 Jan 09 '23 at 12:17
  • @dEitY719 Please see [Why is using 'eval' a bad practice?](https://stackoverflow.com/q/1832940/6045800) and be sure to read more about `eval` if you're actually going to use it. As mentioned in another comment, it would help if you gave context of what you're trying to do because solving it this way is not the best... – Tomerikoo Jan 09 '23 at 12:20
  • @ Tomerikoo Honestly, I can't understand exactly why using eval is a bad habit. I am currently in the process of learning and practicing Python, and I will save your advice and look at it again later. Thank you again for your deep insight. :) – dEitY719 Jan 09 '23 at 12:28
  • @dEitY719 It's a security vulnerability. If a user or external actor has ANY way to control the text being evaluated, they can do almost anything. Even when you're sure there's no way, changes in the future may accidentally open a security hole. – MatBailie Jan 09 '23 at 12:35

2 Answers2

2

You can't, = in f-string is self-documenting sign which just prints expression before it as is. Doc reference

sudden_appearance
  • 1,968
  • 1
  • 4
  • 15
0

Thanks to @Tomerikoo.

  • His reply gave me a hint. I was able to learn the movements I wanted according to his comments

@Tomerikoo's comment

The only way I can think to make something similar to this work is print(f'{text} = {eval(text)}') and then passing the function strings with the expression, i.e. myprn('a > b')

dEitY719
  • 1
  • 1