Skip to content Skip to sidebar Skip to footer

Pep 8 Warning "do Not Use A Lambda Expression Use A Def" For A Defaultdict Lambda Expression

I am using the below python code to create a dictionary.But I am getting one PEP 8 warning for the dct_structure variable. Warning is: do not use a lambda expression use a def from

Solution 1:

A lambda in Python is essentially an anonymous function with awkward restricted syntax; the warning is just saying that, given that you are assigning it straight to a variable - thus giving the lambda a name -, you could just use a def, which has clearer syntax and bakes the function name in the function object, which gives better diagnostics.

You may rewrite your snippet as

def dct_structure():
    returndefaultdict(dct_structure) 

dct = dct_structure() 

Post a Comment for "Pep 8 Warning "do Not Use A Lambda Expression Use A Def" For A Defaultdict Lambda Expression"