dorintufar
dorintufar

Reputation: 650

Python callable argument in **kwargs


I have following code:

def aggregate(self, **kwargs):
    for node in self:
        for prop, val in kwargs.values():
            set_val = val(node) if callable(val) else val
            setattr(node, prop, set_val)
    return self

When i'm trying to set in **kwargs a callable variable, function throw this error:

Message: TypeError("'function' object is not iterable",)

Please help. Can't understand where the problem is
Method call are following: obj.aggregate(my_key=_test) where _test are callable

Upvotes: 2

Views: 178

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155724

You're iterating .values(), where each value is a single item, but you're unpacking to two names, prop and val. The error occurs when it effectively tries to assign prop, val = _test.

Looks like you meant to iterate .items(), not .values(). That way, prop would be "my_key", and val _test as expected.

Upvotes: 7

Related Questions