I am running into this situation while using chained methods in python. Suppose, I have the following code
hash = {}
key = 'a'
val = 'A'
hash[key] = hash.get(key, []).append(val)
The hash.get(key, []) returns a [] and I was expecting that dictionary would be {'a': ['A']}. However the dictionary is set as {'a': None}.
On looking this up further, I realised that this was occurring due to python lists.
list_variable = []
list_variable.append(val)
sets the list_variable as ['A']
However, setting a list in the initial declaration
list_variable = [].append(val)
type(list_variable)
<type 'NoneType'>
What is wrong with my understanding and expectation that list_variable should contain ['A'] Why are the statements behaving differently?
listas a variable name, it shadows the built-inlist()method.