What does any = lambda v: v mean? It seems v is only v itself.
class Object(object):
"""Common base class supporting automatic kwargs->attributes handling,
and cloning."""
attrs = ()
def __init__(self, *args, **kwargs):
any = lambda v: v
for name, type_ in self.attrs:
value = kwargs.get(name)
if value is not None:
setattr(self, name, (type_ or any)(value))
else:
try:
getattr(self, name)
except AttributeError:
setattr(self, name, None)
lambda v: vcreates an identity function, which just returns its argument unchanged. Assigning it to a local variable is equivalent to defining a local function like this:It can be useful as a fallback for code that wants to call a function to do some processing on the argument, for the cases where the real function is not available, or the processing is undesired.
In the code you posted,
type_can be logically false (most likelyNone), which means that it is not to be called, so it’s replaced with an identity function. The author could also have used a more explicitifto skip the function call in that case, at the price of additional clutter in the loop.BTW
anyis a bad name for a local variable because it shadows the built-in function with the same name and a completely different meaning.