In Python 3 I am trying to print the outputs of a **kwargs
using a list comprehension. I am unable to do so while using a for
loop does print the elements of my list input. Below is the reproducible code:
Using list comprehension:
class Practice(object):
__acceptable_keys_list = ['Right', 'left']
def __init__(self, **kwargs):
temp = ([self.__setattr__(key, kwargs.get(key)) for key in self.__acceptable_keys_list])
print(temp)
Output is [None, None]
.
Where as using a for
loop:
class Practice(object):
__acceptable_keys_list = ['Right', 'Left']
def __init__(self, **kwargs):
for key in self.__acceptable_keys_list:
self.__setattr__(key,kwargs.get(key))
print(key)
Output is [Right, Left]
.
Why the difference ? What am I missing ? Shouldn't list comp and for loops behave in similar manner?
Edit: Why the downvotes ? I am trying to understand things here.