I'm a relative python beginner, now fairly comfortable with the language but still grappling over issues of what's "pythonic" and not. I was wondering what people's thoughts on this issue are.
For example, take this line of code for calculating the average cost per week of rental properties drawn from a peewee database:
rental_avg_costperweek = sum([calcCostPerWeek(rental.price, rental.rental_freq) for rental in [LLSRental.select(LLSRental.id == this_id) for this_id in closest_rental_ids]]) \ / len(closest_rental_ids)
It uses nested list comprehensions, which might be confusing.
Alternatively I could stick the inner comprehension into a temporary variable:
closest_rental_records = [LLSRental.select(LLSRental.id == this_id) for this_id in closest_rental_ids] rental_avg_costperweek = sum([calcCostPerWeek(rental.price, rental.rental_freq) for rental in closest_rental_records]) \ / len(closest_rental_ids)
This might be (a little) easier to read, but being an ex-C++ programmer I have an aversion to creating temporary variables purely for readability, as it clutters the namespace and potentially makes more work for the garbage collector.
Also I think if a reader doesn't understand the variable is purely temporary it may make the code more confusing if there are many such variables.
As such, I'm inclined to go with the first option over the second despite the guidance of "flat is better than nested"...but what do ye python veterans think?
Thanks!
gs.