Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python Gotchas

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Python Gotchas

Presented on PythonRio meetup.

Avatar for Erick Mendonça

Erick Mendonça

August 26, 2017
Tweet

More Decks by Erick Mendonça

Other Decks in Programming

Transcript

  1. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

    [0, 1, 1, 3, 4, 5, 6, 7, 8, 9] [0, 1, 1, 3, 4, 5, 6, 7, 8, 9] [0, 1, 1, 3, 1, 5, 6, 7, 8, 9] [0, 1, 1, 3, 1, 5, 6, 7, 8, 9] [0, 1, 1, 3, 1, 5, 1, 7, 8, 9] [0, 1, 1, 3, 1, 5, 1, 7, 8, 9] [0, 1, 1, 3, 1, 5, 1, 7, 1, 9] [0, 1, 1, 3, 1, 5, 1, 7, 1, 9] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 4, in wrong_loop IndexError: list assignment index out of range
  2. def wrong_loop_2(): a = list(range(10)) for i in a: if

    i % 2: a.append(i) print(len(a)) wrong_loop_2()
  3. def create_multipliers(): return [ lambda x : i * x

    for i in range(5) ] for multiplier in create_multipliers(): print multiplier(2) Source: http://docs.python-guide.org/en/latest/writing/gotchas/
  4. data = { 'full_name': 'Bruce Banner', 'birth_date': '1962-05-01', # a

    lot of other data } cache_id = hash(data['full_name']) | hash(data['birth_date']) cache.set(cache_id, data)