ADVANCED PYTHON CONCEPTS

What we'll cover

  • List Comprehensions and Generator Expressions
  • Generators and Iterators
  • Context Managers (with)
  • Decorators
  • Magic Methods and Attributes
  • Functional Style
  • Metaprogramming

other stuff

  • There will be optional assignments for the end of each segments. I recommend that you do them however, they'll really help solidify the things we cover.
  • If you don't understand something I've said, feel free to ask away.
  • I'll upload model answers for the previous lecture after each lecture. If that sentence even makes sense.
  • It might be a good idea to use something like project euler to exercise any skills you learn a little more.
  • Sometimes code on the slides might not be copied verbatim. But it does all work. As far as I know. Please tell me if it doesn't.

List comprehensions

  • Syntactic sugar for building lists
  • Build and modify lists quickly
  • Can act like map & filter rolled into one

General form:
 [item for item in iterable]

FOR those not familiar with map and filter

 >>> my_list = [1, 2, 3] >>> def add_one(n): ...     return n + 1 >>> map(add_one, my_list) [2, 3, 4]
>>> def is_even(n): return True if n%2 == 0 else False >>> filter(is_even, my_list) [2]
>>> filter(is_even, map(add_one, my_list)) [2, 4]

also, a little aside

I'm going to be using lambdas quite a lot for simple functions. They generally look like this:
 >>> f = lambda arguments: returnvalue
Left side of colon is comma-delimited arguments, right side is an expression that is evaluated and then returned.

I can rewrite the add_one function from the last page in this form:
 >>> add_one = lambda n: n + 1 >>> add_one(1) 2

more lambdas

And remember you don't actually need to assign them to anything either. they're just a quick way of creating a function object.

 >>> (lambda n: n*2)(4) 8
>>> map(lambda n: n%2, [1,2,3,4]) [1,0,1,0]

anyway, back to list comprehensions

  • The most basic use is generating flat lists
 >>> [i for i in range(5)] [0, 1, 2, 3, 4]
  • But we can also apply some operation to the items of a list
 >>> [i**2 for i in range(5)] [0, 1, 4, 9, 16]
  • Or even remove ones we don't want
 >>> [i for i in range(5) if i%2==0] [0, 2, 4]
  • And any combination of these
 >>> [i**2 for i in range(5) if i%2==0] [0, 4, 16]

here's something a bit stranger

What will this return?
 >>> [(i,j) for i in (0,1) for j in (0,1)]

DICTCOMPS are pretty similar

 >>> square_lookups = {thing:thing**2 for thing in range(3)} {0:0, 1:1, 2:4}
>>> square_lookups[2] 4

And the same for sets too

 >>> [i*j for i in range(3) for j in range(3)]
 [0, 0, 0, 0, 1, 2, 0, 2, 4]
>>> {i*j for i in range(3) for j in range(3)} set([0, 1, 2, 4])

pitfall: tuples

  • This:
 >>> tuple(i for i in range(3)) (0, 1, 2)
  • Not this:
 >>> (i for i in range(3)) <generator object <genexpr> at 0x...>

ADVANCED PYTHON CONCEPTS

By Laurence Smith

ADVANCED PYTHON CONCEPTS

Lectures on the use of several advanced python concepts.

  • 444