Dat*_*ery -1 python methods list
I thought in Python I am allowed to perform method chaining.
basket = [1,3,2,4,6,8]
basket.append(7)
basket.sort()
basket.reverse()
Run Code Online (Sandbox Code Playgroud)
This works.
basket.append(7).sort().reverse()
Run Code Online (Sandbox Code Playgroud)
This does not.
AttributeError: 'NoneType' object has no attribute 'sort'
Run Code Online (Sandbox Code Playgroud)
I am not sure what is going on here, but I assume that happens because in place methods result in "NoneType" result = basket.sort() and therefore the second method will be performed on the result and not the original object.
Can anyone help me how to do these operations without writing a new line for each method?
Because append sort and reverse are all "in-place" methods, so they don't return anything, instead they update the original list, the best way would be:
print(sorted(basket + [7], reverse=True))
Run Code Online (Sandbox Code Playgroud)