Issue
How can I do the following in a more concise, "pythonic" way:
for element in some_list:
if some_condition:
element.some_attr = some_value
Solution
The only thing that's not pythonic in your code is that you're not using 4 spaces for indentation.
“Pythonic” doesn't always mean “concise”; for example the following is shorter, but less readable than your loop:
[setattr(e, 'some_attr', some_value) for e in some_list if some_condition]
So, stick to the code you have.
Answered By - Petr Viktorin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.