I'm trying use python's reduce in a similar way to racket's foldl, but when I run the following code:
functools.reduce(lambda x, y: x.append(y), [1,2,3], [])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'append'
Can you please help explain the error and suggest a fix?
reduceexpects a function argument that returns something, whichappenddoesn't....append()is an in-place operation, so always returnsNone? And are you aware that you don't have another reference to this list, so even if you did.append()to it, the changes would be lost? Why are you breaking up[1,2,3]into elements instead of doing them all at once with.extend([1,2,3])? Why are you using an empty list in the first place when you could just use[1,2,3]?__init__method being called here, if that's what you mean.functools.reduceisn't a class, if that's what you thought; it's just a function.