-4

I have a function in which variables of 'node1', 'node2', 'node3' are referenced.

I would like to make it so that the second time the function is called, 'node1' become 'node1_a' and on the third time 'node1_b' and so on.

These 'dynamic' variables are referenced only within the function. I have some code below if it helps understanding.

def marriage(husband, wife):
    count += 1
    node1 = pydot.Node(str(husband))
    node3 = pydot.Node('node_a', label='', style = 'invisible', shape = 'circle', width = '.001', height = '.001')
    node2 = pydot.Node(str(wife))
    tree.add_edge(pydot.Edge(node1, node3, style = "dashed"))
    tree.add_edge(pydot.Edge(node3, node2, style = "dashed"))
Sam
  • 1,052
  • 4
  • 13
  • 33
  • 3
    Sounds like what you really need is a data structure like a list or dict. – kirbyfan64sos Jun 07 '15 at 20:20
  • Can you have a counter outside the function which counts how many times the function is called? If yes, pass that counter inside the function and use a dict to generate variable names as keys. – Vikas Ojha Jun 07 '15 at 20:20
  • Did you read *any* of the related questions that popped up when you wrote your title and question? This topic has been covered hundreds of times here... – MattDMo Jun 07 '15 at 20:24
  • I have a counter outside the function. I also have a list of the variable names. How do I assign the list elements as variable names? – Sam Jun 07 '15 at 20:26
  • @SamuelBancroft do what the first comment suggested: use a dict. – MattDMo Jun 07 '15 at 20:27

2 Answers2

0

I think you can try dict the key is var name and the value is var value. The calltimeindex should be changed every time function called.

keyName = [["node"+str(index)+"_"+ chr(i) for i in range(ord('a'),ord('z'))] for index in range(1,4)]

varDict[keyName[varIndex][callTimeIndex]] = value
galaxyan
  • 5,944
  • 2
  • 19
  • 43
0
def marriage(husband, wife, method_dict):
    count += 1
    method_dict['node1'][count] = pydot.Node(str(husband))
    method_dict['node3'][count]node3 = pydot.Node('node_a', label='', style = 'invisible', shape = 'circle', width = '.001', height = '.001')
    method_dict['node2'][count]node2 = pydot.Node(str(wife))
    tree.add_edge(pydot.Edge(method_dict['node1'][count], method_dict['node3'][count], style = "dashed"))
    tree.add_edge(pydot.Edge(method_dict['node3'][count], method_dict['node2'][count], style = "dashed"))

where

method_dict = {{}}
Endzior
  • 164
  • 9