-2

I have 2 python files 1 where only dictionary is saved Example :

Dic.py

A={"1":{"a":1},    "2":{"b":2}}

And calldic.py

import dic

def todo(Value,Index):
    Data=dic.Value(Index)
    Print(Data)

As in the above code I am trying to pass argument as member of the dic.py file.

Can anyone please help me resolve this or correct me where I am wrong.

Thanks in advance

cs95
  • 379,657
  • 97
  • 704
  • 746
user3724559
  • 219
  • 1
  • 6
  • 20
  • What exactly are you trying to achieve here? Also, python is case-sensitive. – Axe319 Dec 23 '20 at 11:33
  • I don't want to write code for each key in the dictionary I want to have it as dynamic , in the above code if I call todo method as todo(A,1) Value = A and Index = 1 it should fetch the data in it. – user3724559 Dec 23 '20 at 11:38
  • Please do not rate it down as I am posting this from my mobile and have restrictions example control key – user3724559 Dec 23 '20 at 11:40

1 Answers1

1

If what I infer from your question is correct, this should work (assuming Value="A"):

variable_name = Value
attr = getattr(dic, variable_name)
data = attr.get(index)

or

data = attr[index]

Convert index to string before if you are passing index as an integer:

index = str(index)

Edit: As mentioned in the comment, you can use the dict attribute on modules/classes as well.

data = dic.__dict__[value][index]
Jarvis
  • 8,494
  • 3
  • 27
  • 58