Search code examples
pythonparameter-passingdefaultdict

Python unable to pass defaultdict values to function


I have a complex code which reads some values into nested defaultdict. Then there is a cycle going through the keys in the dictionary and working with them - basically assigning them to another nested defaultdict.

Problem is, when I want to use the values from the dictionary and access them and pass them as values to a function.... I get either empty {} or something like this: defaultdict(<function tree at 0x2aff774309d8>

I have tried to write the dict so I can see if it is really empty. Part of my code;

if (not families_data[family]['cell_db']['output']):
        print(rf"Output for {family} is  empty.")
        print(dict(families_data[family]['celldb']))

The really fun part is, when this "if" is true, then I get the following output:

Output for adfull is empty. {'name': 'adfullx05_b', 'family': 'adfull', 'drive_strength': 0.5, 'template': 'adfull', 'category': '', 'pinmap': '', 'output': 'CO S', 'inout': '', 'input': 'A B CI', 'rail_supply': 'VDD VSS', 'well_supply': '', 'description': ''}

if I change the second line in the if to

        print(families_data[family]['celldb'])

I get the following output:

defaultdict(<function tree at 0x2b45844059d8>, {'name': 'adfullx05_b', 'family': 'adfull', 'drive_strength': 0.5, 'template': 'adfull', 'category': '', 'pinmap': '', 'output': 'CO S', 'inout': '', 'input': 'A B CI', 'rail_supply': 'VDD VSS', 'well_supply': '', 'description': ''})

Why is the "if" even true, when there is a value 'CO S' in the output key?

Why am I getting {} when trying to access any value like families_data[family]['cell_db']['input'] and passing it to function as a parameter?

What the heck am I doing wrong?


Solution

  • The "cell_db" key in the if statement has an underscore while it does not in the print statement.

    This should fix it:

    if (not families_data[family]['celldb']['output']):
        print(rf"Output for {family} is  empty.")
        print(dict(families_data[family]['celldb']))