Idiomatic Python

2,520 views
2,321 views

Published on

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

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,520
On SlideShare
0
From Embeds
0
Number of Embeds
91
Actions
Shares
0
Downloads
47
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Idiomatic Python

  1. 1. Idiomatic Python P.C. Shyamshankar PyCon India 2009 September 25, 2009 P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 1 / 35
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. Putting strings together. Strings are immutable String addition creates copies. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 7 / 35
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. Enter Iterators Maintain state. Transit between states. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 19 / 35
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. They’re short. What else? Comprehensions are fast. Faster than looping. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 25 / 35
  26. 26. 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
  27. 27. Motivation A common pattern: Setup Try Except Finally P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 27 / 35
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. 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
  33. 33. 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
  34. 34. 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
  35. 35. Thank you. That’s it. P.C. Shyamshankar (PyCon India 2009) Idiomatic Python September 25, 2009 35 / 35

×