amontalenti / elements-of-python-style

Goes beyond PEP8 to discuss what makes Python code feel great. A Strunk & White for Python.
3.45k stars 258 forks source link

"Good" example for "Flat is better than nested" can be optimized #24

Closed ulope closed 8 years ago

ulope commented 8 years ago

The "good" example for "Flat is better than nested" could be optimized to:

if response:
    return response.get('data')

But since the goal is probably to demonstrate the merged conditions another example would probably be better.

amontalenti commented 8 years ago

@ulope That's a good point. I can probably just tweak the example slightly to make this optimization impossible. For example, have it return len(response["data"]), rather than returning response["data"]; the len call would fail on NoneType. Then the function would be one that returns None if response["data"] is missing or empty, and returns its length (int > 0) if it is present and has items.