A few things to remember while coding in Python
satyajit.ranjeev.in
A few things to remember while coding in Python
1–10 of 146 posts
Re: A few things to remember while coding in Python
#2 varname, = [x for x in l if predicate_with_single_truth_value(x)]
The comma after varname is an implicit assert that the list comprehension only contains one element.Re: A few things to remember while coding in Python
#3Re: A few things to remember while coding in Python
#4Another handy one I saw recently: varname, = [x for x in l if predicate_with_single_truth_value(x)] The comma after varname is an implicit assert that the list comprehension only contains one element.
This sort of code would be very confusing when I'm just quickly reading through a procedure trying to find the potential bug.
Re: A few things to remember while coding in Python
#5The problem with mutable defaults is that they are evaluated once only when the function is defined. Each time the function is called you'll be using the same mutable variable that was created during function definition.
Re: A few things to remember while coding in Python
#6Another handy one I saw recently: varname, = [x for x in l if predicate_with_single_truth_value(x)] The comma after varname is an implicit assert that the list comprehension only contains one element.
Trailing commas are really easy to miss. When reading this line of code, I did not notice it immediately; I originally assumed that varname was being assigned a list. This sort of code would be very confusing when I'm just quickly reading through a procedure trying to find the potential bug.
(varname,) = [x for x in l if predicate_with_single_truth_value(x)]Re: A few things to remember while coding in Python
#7Another handy one I saw recently: varname, = [x for x in l if predicate_with_single_truth_value(x)] The comma after varname is an implicit assert that the list comprehension only contains one element.
Trailing commas are really easy to miss. When reading this line of code, I did not notice it immediately; I originally assumed that varname was being assigned a list. This sort of code would be very confusing when I'm just quickly reading through a procedure trying to find the potential bug.
[varname] = [x for x in l if predicate_with_single_truth_value(x)]Re: A few things to remember while coding in Python
#8Another handy one I saw recently: varname, = [x for x in l if predicate_with_single_truth_value(x)] The comma after varname is an implicit assert that the list comprehension only contains one element.
Trailing commas are really easy to miss. When reading this line of code, I did not notice it immediately; I originally assumed that varname was being assigned a list. This sort of code would be very confusing when I'm just quickly reading through a procedure trying to find the potential bug.
varname ,= [...]
Re: A few things to remember while coding in Python
#9[1] http://stackoverflow.com/questions/118370/how-do-you-use-the...