Slide 1

Slide 1 text

Python Tricks Steven Pineda Cortés Based on Dan Bader’s book

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Most simple useful advice: a Comma

Slide 4

Slide 4 text

Make sure you use assertions “The proper use of assertions is to inform developers about unrecoverable errors in a program”.

Slide 5

Slide 5 text

Common errors with assertions 1. Don’t use it for data validation, use regular Exceptions. 2. Asserts which never fail.

Slide 6

Slide 6 text

Context Managers

Slide 7

Slide 7 text

Context Managers

Slide 8

Slide 8 text

Context Managers, when to use? ● Safe acquisition and release of system resources. ● Avoids resource leaks and makes code easier to read.

Slide 9

Slide 9 text

Underscores, Dunders and More

Slide 10

Slide 10 text

String formatting

Slide 11

Slide 11 text

String formatting, which to use? if strings_are_user_supplied: use = “TemplateStrings” elif PYTHON_VERSION >= 3.6: use = “Interoplation” else: use = “New style”

Slide 12

Slide 12 text

Functions are first-class citizens 1. Functions are objects (everything is). 2. Functions can be stored in data structures. 3. Functions can be passed to other functions! (high-order functions). 4. Functions can be nested. 5. Functions can capture local state. 6. Objects can behave like functions.

Slide 13

Slide 13 text

Functions are first-class citizens

Slide 14

Slide 14 text

Functions are first-class citizens

Slide 15

Slide 15 text

Lambda functions ● Shortcut for declaring small anonymous functions. ● Restricted to a single expression. ● Don’t go crazy with them

Slide 16

Slide 16 text

Decorators ● Allow you to extend and modify the behavior of a callable without permanently modifying the callable itself. ● Any sufficiently generic functionality is a great candidate: ○ Logging ○ Enforcing access control and authentication ○ Instrumentation and timing functions. ○ Rate-limiting. ○ Caching. ○ More!

Slide 17

Slide 17 text

Decorators

Slide 18

Slide 18 text

Decorators

Slide 19

Slide 19 text

*args and **kwargs ● You can create more flexible APIs with variable number of arguments. ● Essential for decorators and other wrappers. ● If we call the function with additional arguments, args will collect them as a tuple. ● Likewise, kwargs will collect extra keyword arguments as a dictionary.

Slide 20

Slide 20 text

*args and **kwargs

Slide 21

Slide 21 text

Argument unpacking

Slide 22

Slide 22 text

“is” vs “==”

Slide 23

Slide 23 text

Every class needs a __repr__ ● Python fallsback to repr if str is not defined. ● print and format use str. ● str should be readable. ● repr should be unambiguous.

Slide 24

Slide 24 text

Defining your own Exceptions ● First of all, use Exceptions! ● Asking for permission instead of forgiveness. ● Custom exceptions make it clearer. ● You can use inheritance to have a hierarchy.

Slide 25

Slide 25 text

Cloning objects ● Beware of shallow copying. ● Use copy module for deep copies. ● It works even for arbitrary objects

Slide 26

Slide 26 text

Cloning objects, for real.

Slide 27

Slide 27 text

Abstract classes ● Abstract classes are useful. ● Python is not very strict. ● We are human. ● The sooner we fail, the better.

Slide 28

Slide 28 text

Abstract classes ● ABC ensures that derived classes implement methods from the base class. ● Using ABC helps avoiding bugs and makes class hierarchies easier to maintain.

Slide 29

Slide 29 text

USE PEP-8 https://www.python.org/dev/peps/pep-0008/

Slide 30

Slide 30 text

Thanks!