0

I have a requirement on cocotb similar to below :

x = "a"
x = 2 => This should become a = 2

Can someone please help whether would it be possible to achieve this in python ? I need to assign values to DUT (like below) based on the above approach:

for sig in ["sig_1", "sig_2"]:
    self.bfm.sig = 1
Somesh
  • 82
  • 8

3 Answers3

1

I’m pretty sure you don’t want to be doing this in your code, but what you are trying to do can be accomplished using eval():

for sig in ["sig_1", "sig_2"]:
    eval(f”self.bfm.{sig} = 1”)

This will also work with your MWE at the top of your question:

x = "a"
eval(f”{x} = 2”)

Note that this kind of use (or abuse) of eval goes against best practices. You’d probably be better off turning bfm into a dict, which is frankly made to accept strings as keys in the way you are trying to use them:

for sig in ["sig_1", "sig_2"]:
    self.bfm[sig] = 1
Drphoton
  • 164
  • 9
0

as Unmitigated commented:

for sig in ["sig_1", "sig_2"]:
    self.bfm[sig]=1

should do if you want 1 for both of them "sig_1", "sig_2"

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

Use square brackets.

for sig in ["sig_1", "sig_2"]:
    self.bfm[sig] = 1
Unmitigated
  • 76,500
  • 11
  • 62
  • 80