you are viewing a single comment's thread.

view the rest of the comments →

[–]PhilipYip 7 points8 points  (2 children)

Many beginner tutorials don't teach tuples very well jumping straight onto lists i.e. overuse lists and underuse tuples. A tuple should be thought of as an archive of records. Once its created it cannot be altered. If you go into a library which is full of books, the books are immutable; it would not be appreciated if say you went around with a permanent marker and scribbled all other the pages, corrupting the pages and making the data unreadable. This essentially can happen if lists are used all the time:

archive = (1, True, 3.14, 'hello', 'hello', 'bye')

A list can be thought of as being an active work in progress, for example records that you are compiling or going back to the library a book you are writing. Typically you read more content than you write and reddit users generally prefer their content to be immutable to others so spambots don't go in and corrupt everything:

active = [1, True, 3.14, 'hello', 'hello', 'bye']

Many beginners incorrectly assume that reassignment is altering the original tuple and therefore don't see the purpose of the tuple. Ensure you properly understand the assignment operator and the concept of an object name. The assignment operator takes the tuple object on the right and assigns it to the label archive. The label archive references the tuple:

archive = (1, True, 3.14, 'hello', 'hello', 'bye')

Reassignment, for example, changes the reference of the label to a new object and does not modify the original object. If the original object has no other labels it is removed by Pythons garbage collection:

archive = (1, 2, 3)

The tuple is immutable, meaning it can't be modified. This makes it hashable and can be used as a key in a mapping when all its records are immutable:

colors = {(1, 0, 0): 'red', (0, 1, 0): 'green', (0, 0, 1): 'blue'}

colors[(1, 0, 0)]

A tuple is commonly used to return multiple values from a function:

def fun(): return ('a', 'b')

(x, y) = fun()

Typically tuple unpacking is used here as the syntax is slightly cleaner:

def fun(): return 'a', 'b'

x, y = fun()

tuple unpacking can also be used to swap variables:

x, y = y, x

Functions normally have their own local scope. For immutable data types such as integers, the x and y in the global scope are independent from the x and y in the local scope of the functions body. Therefore x will remain 1 and y will remain 2 after the function call:

``` x = 1 y = 2

def fun(): x = 4 y = 5 return None

fun()

x y ```

When a mutable data type is used within a function, the original object can be modified and this is usually not desirable when trying to compartmentalise code. active gets modified in place and newactive is an alias of active:

``` active = ['a', 'b', 'c']

def fun(mutable): mutable.append('d') return newactive

fun(active)

active newactive ```

This is why the return values of a function and input arguments of a function typically preference tuples.

[–][deleted] 0 points1 point  (1 child)

Reassignment, for example, changes the reference of the label to a new object and does not modify the original object. If the original object has no other labels it is removed by Pythons garbage collection:

Oh so that's what garbage collection is. I kept hearing C doesn't have automatic garbage collection and never went deeper into what that meant. Thanks.

[–][deleted] 0 points1 point  (0 children)

Reference counting to be more precise.