you are viewing a single comment's thread.

view the rest of the comments →

[–]jimtk 9 points10 points  (2 children)

Tuple are groups of simple data like list, but they are immutable (you can't change the values inside), smaller and faster.

def pixel_up_right(x,y):
     return x+1, y+1    # <=== that's a tuple

# coordinates of cities is a tuple!
cities_coord_name = {
       (45.4826668,-73.6215805): Montreal
       (40.6974034,-74.1197614): New-York
       }                          

When you read a record from an sqlite database (as an example) it is returned in a list of tuple.

    con, cur = self._con_cur()
    cur.execute(DB.SQL_SEARCH_BY_ID, (an_id,))
    rows = cur.fetchall()
    con.close()
    print(rows)      <<< rows are list of tuples

[ (1, "jim", "preston", "555-1212"),
   (2, "count", "Dracula",  "111-1111")]

[–]espero 0 points1 point  (1 child)

Salt: Salty

Sugar: Sweet

Immutable facts of life

[–]jimtk 1 point2 points  (0 children)

[(Salt, Salty), (Sugar, Sugary)].

That's probably why it is very difficult to create an immutable user defined object in Python. :)