Skip to content

Instantly share code, notes, and snippets.

@brun0xff
Last active July 4, 2018 20:50
Show Gist options
  • Save brun0xff/be8dc463f74fd8222a73084dcb36f28a to your computer and use it in GitHub Desktop.
Save brun0xff/be8dc463f74fd8222a73084dcb36f28a to your computer and use it in GitHub Desktop.

Uma boa prática é ao capturar exceções, mencionar sempre que possível as exceções específicas, em vez de usar apenas except Exception. Exemplo:

try:
    import platform_specific_module
except ImportError:
    platform_specific_module = None

Dá uma olhada:

A good rule of thumb is to limit use of bare 'except' clauses to two cases:

1. If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred. 2. If the code needs to do some cleanup work, but then lets the exception propagate upwards with raise. try...finally can be a better way to handle this case.

Fonte: https://www.python.org/dev/peps/pep-0008/?

É preferível usar list comprehension em vez de filter/map quando uma lista é esperada como retorno, especialmente quando o código original usa lambda.

Dá uma olhada:

map() and filter() return iterators. If you really need a list, a quick fix is e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).

Fonte: https://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment