0

I was wondering if I can pass an argument to a function by passing it a different argument that constructs the first argument's name from strings. Basically cut(arg) to be the same as cut(clip_a)?

str = ['a', 'b']

clip_a = ['vacation', 'work']
clib_b = ['gym', 'court']

arg = f'clip_{str[0]}'

def cut(clip):
    clip[0] = 'sauna'

cut(arg)
cosmic89
  • 74
  • 1
  • 4
  • If the linked question doesn't help you, you can access variables by their string name with `globals()[name]`. `globals()` returns a dict-like object that contains the global variables. Similarly, `locals()` gives access to local variables. If `clip` is a string name then you could change your function to contain `globals()[clip][0] = "sauna"` – MichaelCG8 Apr 27 '22 at 16:26
  • Thank you @MichaelCG8 You helped me a lot with this. – cosmic89 Apr 27 '22 at 17:19

0 Answers0