Idiomatic Python

35
Idiomatic Python P.C. Shyamshankar PyCon India 2009 September 25, 2009 P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 1 / 35

description

A talk given at PyCon India 2009, in Bangalore, India on 25th September, 2009

Transcript of Idiomatic Python

Page 1: Idiomatic Python

Idiomatic Python

P.C. Shyamshankar

PyCon India 2009

September 25, 2009

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 1 / 35

Page 2: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 2 / 35

Page 3: Idiomatic Python

The Zen of Python, by Tim Peters

Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren’t special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one, and preferably only one, obvious way to do it.Although that way may not be obvious at first unless you’re Dutch.Now is better than never.Although never is often better than right now.If the implementation is hard to explain, it’s a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea – let’s do more of those!

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 3 / 35

Page 4: Idiomatic Python

Idioms

An Unwritten Rule

A common use-case.

Usually to make the code better in some way:I ReadabilityI SpeedI Resource Usage

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 4 / 35

Page 5: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 5 / 35

Page 6: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 6 / 35

Page 7: Idiomatic Python

Putting strings together.

Strings are immutable

String addition creates copies.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 7 / 35

Page 8: Idiomatic Python

Join, Don’t Add.

s = ’’for i in list_of_strings :

s += i

s = ’’.join(list_of_strings)

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 8 / 35

Page 9: Idiomatic Python

That’s just what everyone says.

String addition isn’t evil, just asymptotically slow.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 9 / 35

Page 10: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 10 / 35

Page 11: Idiomatic Python

EAFP

It’s Easier to Ask Forgiveness than Permission.

Asking permission is to LBYL

But it depends on the context.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 11 / 35

Page 12: Idiomatic Python

A Story of Keys in a Dict

If the key is in the dict, increment its value.

If not, set its value to 1.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 12 / 35

Page 13: Idiomatic Python

Asking for Permission

if key in my_dict :my_dict[key] += 1

else :my_dict[key] == 1

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 13 / 35

Page 14: Idiomatic Python

Asking for Forgiveness

try :my_dict[key] += 1

except KeyError :my_dict[key] = 1

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 14 / 35

Page 15: Idiomatic Python

Choosing the right time

Use the first approach, if you’re more likely to succeed.

Use the second if you’re more likely to fail.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 15 / 35

Page 16: Idiomatic Python

So many choices!

my dict.get(key, 0) += 1

my dict.setdefault(key, []).append(value)

my dict = collections.defaultdict(int)

my dict = collections.Counter()

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 16 / 35

Page 17: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 17 / 35

Page 18: Idiomatic Python

Be Lazy!

Compute only what you need to compute. It saves time.

Store only what you need to store. It saves space.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 18 / 35

Page 19: Idiomatic Python

Enter Iterators

Maintain state.

Transit between states.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 19 / 35

Page 20: Idiomatic Python

A simple Fibonacci Iterator

class Fibonacci :def __init__(self) :

self.p, self.q = 0, 1def __iter__(self) :

return self

def __next__(self) :self.p, self.q = self.q, self.p + self.qreturn self.p

f = Fibonacci()for i in range(10) :

print(next(f))

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 20 / 35

Page 21: Idiomatic Python

Enter Generators

An easier way to be lazy.

Define functions, but use yield instead of return.

The generator picks up where it left off.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 21 / 35

Page 22: Idiomatic Python

A simpler Fibonacci Generator

def Fibonacci() :p, q = 0, 1

while True :p, q = q, p + qyield p

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 22 / 35

Page 23: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 23 / 35

Page 24: Idiomatic Python

What are comprehensions?

A short-hand form for building:I Lists [func(i) for i in sequence]I Generators (func(i) for i in sequence)I Sets {func(i) for i in sequence}I Dicts {key : func(key) for key in sequence}

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 24 / 35

Page 25: Idiomatic Python

They’re short. What else?

Comprehensions are fast.

Faster than looping.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 25 / 35

Page 26: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 26 / 35

Page 27: Idiomatic Python

Motivation

A common pattern:I SetupI TryI ExceptI Finally

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 27 / 35

Page 28: Idiomatic Python

A common use case

connection = connection_to_db(db_info)

try :do_some_stuff_with_db(connection, data)

except BorkedOperation :handle_it()

finally :clean_up_the_mess()

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 28 / 35

Page 29: Idiomatic Python

The ”with” statement.

Simplifies the above usage pattern.

def insert_data(db_info, data) :with db_connection(db_info) as connection :

do_some_stuff_with_db(db_info, data)

Much simpler.

But where did the complexity go?

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 29 / 35

Page 30: Idiomatic Python

Context Managers

Classes that implement enter and exit methods.

Encapsulates the relevant logic within these methods.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 30 / 35

Page 31: Idiomatic Python

In Context

enter method of the context manager is called, result stored invar.

Block is executed.

exit method of the context manager is called.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 31 / 35

Page 32: Idiomatic Python

Outline

1 Introduction

2 IdiomsAccumulationForgivenessLazinessComprehensionContextOdds and Ends

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 32 / 35

Page 33: Idiomatic Python

Odds and Ends

Enumerate, don’t index.

Unpack tuples, don’t index.

Assign to slices, don’t loop over them.

Duck Type, don’t Check Type.

Use your head.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 33 / 35

Page 34: Idiomatic Python

DRY

Don’t Repeat Yourself.

itertools are meant to help you process data.

collections are meant to help you hold data.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 34 / 35

Page 35: Idiomatic Python

Thank you.

That’s it.

P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 35 / 35