• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Idiomatic Python
 

Idiomatic Python

on

  • 3,236 views

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

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

Statistics

Views

Total Views
3,236
Views on SlideShare
3,151
Embed Views
85

Actions

Likes
2
Downloads
43
Comments
0

9 Embeds 85

http://lucentbeing.com 45
http://www.slideshare.net 11
http://www.24mantra.blogspot.com 9
http://24mantra.blogspot.com 6
http://www.lucentbeing.com 6
http://24mantra.blogspot.in 5
http://localhost:8000 1
http://www.docshut.com 1
http://24mantra.blogspot.gr 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Idiomatic Python Idiomatic Python Presentation Transcript

    • Idiomatic Python P.C. Shyamshankar PyCon India 2009 September 25, 2009 P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 1 / 35
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 2 / 35
    • 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
    • Idioms An Unwritten Rule A common use-case. Usually to make the code better in some way: Readability Speed Resource Usage P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 4 / 35
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 5 / 35
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 6 / 35
    • Putting strings together. Strings are immutable String addition creates copies. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 7 / 35
    • 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
    • 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
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 10 / 35
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 17 / 35
    • 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
    • Enter Iterators Maintain state. Transit between states. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 19 / 35
    • A simple Fibonacci Iterator class Fibonacci : def __init__(self) : self.p, self.q = 0, 1 def __iter__(self) : return self def __next__(self) : self.p, self.q = self.q, self.p + self.q return 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
    • 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
    • A simpler Fibonacci Generator def Fibonacci() : p, q = 0, 1 while True : p, q = q, p + q yield p P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 22 / 35
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 23 / 35
    • What are comprehensions? A short-hand form for building: Lists [func(i) for i in sequence] Generators (func(i) for i in sequence) Sets {func(i) for i in sequence} Dicts {key : func(key) for key in sequence} P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 24 / 35
    • They’re short. What else? Comprehensions are fast. Faster than looping. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 25 / 35
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 26 / 35
    • Motivation A common pattern: Setup Try Except Finally P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 27 / 35
    • 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
    • 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
    • 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
    • In Context enter method of the context manager is called, result stored in var. Block is executed. exit method of the context manager is called. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 31 / 35
    • Outline 1 Introduction 2 Idioms Accumulation Forgiveness Laziness Comprehension Context Odds and Ends P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 32 / 35
    • 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
    • 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
    • Thank you. That’s it. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 35 / 35