[Solved] PEP 8 warning “Do not use a lambda expression use a def” for a defaultdict lambda expression [duplicate]


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():
    return defaultdict(dct_structure) 

dct = dct_structure() 

solved PEP 8 warning “Do not use a lambda expression use a def” for a defaultdict lambda expression [duplicate]