Home > Mobile >  python recursion function with dict
python recursion function with dict

Time:11-01

I want to call a dict through a recursive function.
I want to print Goal.
However, I get the following error:(

Variable
asdf =  {'aws_iam_policy': {'vpc_flow_log_cloudwatch': {'__end_line__': 94,
                                                 '__start_line__': 88,
                                                 'count': ['${local.create_flow_log_cloudwatch_iam_role '
                                                           '? 1 : 0}'],
                                                 'name_prefix': ['vpc-flow-log-to-cloudwatch-'],
                                                 'policy': ['${data.aws_iam_policy_document.vpc_flow_log_cloudwatch[0].json}'],
                                                 'tags': ['${merge(var.tags,var.vpc_flow_log_tags)}']}}}
Code
def test(test:dict):
    for key, values in test.items():
        if type(values) is dict:
            test(values)                        # Error TypeError: 'dict' object is not callable
        else :
            print("Goal")
test(asdf)                          
Output
Traceback (most recent call last):
  File "/InitCloud/scan/scan/cand0.py", line 49, in <module>
    test(asdf)                          
  File "/InitCloud/scan/scan/cand0.py", line 45, in test
    test(values)                        # Error TypeError: 'dict' object is not callable
TypeError: 'dict' object is not callable

CodePudding user response:

You have not clarified what you want, but your recursive function is wrong. It must be like this.

def test(a_dic):
    for key, values in a_dic.items():
        if type(values) is dict:
            test(values)
        else: print('Goal')
            
test(asdf)
  • Related