asmeurer / blog

My blog
https://www.asmeurer.com/blog/
8 stars 2 forks source link

Tuples | Aaron Meurer's Blog #11

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Tuples | Aaron Meurer's Blog

https://asmeurer.com/blog/posts/tuples/

asmeurer commented 3 years ago

To comment on this post, click on "Sign in with GitHub" below. If you don't want to authorize the Utterances bot, you can go to the GitHub issues page for this post and comment there.

asmeurer commented 3 years ago

These are the original comments on this post that were made when this blog used the Disqus blog system.

Comment from David Beazley on 2016-09-22 13:54:59+00:00:

Very nice! This post is a good counter-example of everything I meant in my original tweets. Here's what I mean... it's very easy to start a discussion by introducing lists. For example, you make a list of pets or something:

pets = ['cat', 'dog', 'hamster']
pets.append('snake')
print(len(pets))

and then tuples get introduced as a "read-only list":

pets = ('cat', 'dog', 'hamster')
pets.append('snake') # Error. Immutable...

Argh! No. No. No. This is my red-flag. It's the wrong mental model for how tuples usually get used. Tuples have more nuance. Yes, they are list like, but it's way more common to use a tuple as a "record" or "structure" or something where related values get grouped together.

date = (2016, 9, 22)

And as you cover, you might do all sorts of stuff with that:

year, month, day = date # Unpacking in variables
return (year, month, day) # Return multiple values
m = { (year, month, day) : 'meeting' } # Composite dictionary key

I like the parse tree example. It's maybe not a record, but you're definitely not thinking about "lists" there (at least I don't think so).

Replies:

Comment from asmeurer on 2016-09-22 17:15:58+00:00:

If you start with mutable and then say "but what if you want it to be immutable", it's unmotivated, because no one starts off thinking "I need this to be immutable". They might think they need hashablility, but that's distinct concept. The benefits of immutability are more hidden. You aren't, especially as a beginner, going to think you "need" it from the get go. It's better start with immutable, and then motivate "but what if you need mutability".

Comment from Danno on 2016-09-22 14:41:13+00:00:

Your example 1 doesn't show a tuple on the left hand side of the assignment (same for footnote 2). That is actually pattern matching syntax.

For example:

>>> bar=(a,b)=[1,2]
>>> a
1
>>> b
2
>>> bar
[1, 2]

Replies:

Comment from asmeurer on 2016-09-22 16:15:36+00:00:

Yeah I know it's not really a tuple, especially since Python's pattern matching is woefully limited—only identifiers and other "tuples" can go in those parentheses. But it looks like one, and if you're teaching newcomers to the language, it's not too incorrect to say that is one, thanks to the relative syntactic consistency of the language.

Comment from CAChemE on 2016-09-22 16:33:26+00:00:

Excellent post and discussion. For me, tuples clicked in my mind when I read the following comment in SO click:

"Tuples are useful when position has relevance - the best example comes from coordinates in mathematics, which even uses the same syntax: (x, y, z)"

http://stackoverflow.com/qu...

I found that analogy helpful for beginners:
http://swcarpentry.github.i...
What do you think?

Comment from Pierre de Buyl on 2016-09-23 11:25:09+00:00:

Hi,

Your footnote 4, "t = (1,2, [3, 4]) ; t[2] += [5,6]" raises an error but actually ends up modifying "t".

Just tested in the REPL on Python 2.7.11 and 3.5.1.

Comment from Denis Akhiyarov on 2016-10-15 03:29:01+00:00:

Why there is no tuple comprehension?

Comment from Yoong Kang Lim on 2017-01-01 14:48:49+00:00:

The "tuple as record" argument is strengthened when one considers how similar a namedtuple is with a Struct in other languages, like Ruby:

Ruby:

Struct.new('Person', :name, :age)
person = Struct::Person.new('Alice', 23)

Python:
from collections import namedtuple

Person = namedtuple('Person', ['name', 'age'])
person = Person('Alice', 23)