[Python-talk] Reflections on tuples, September 2005, 1 of 2

Bill Sconce sconce at in-spec-inc.com
Fri Sep 23 17:41:29 EDT 2005


----------------------------------
PySIG September 2005
Tuple Snippets, 1 of 2
Brackets around an Lvalue
----------------------------------

Many thanks to Lloyd Kvam for last night's presentation.  Connecting
Python with its roots helps me understand it better.  For some reason
it seems to be a special kind of fun to dissect how Python is made,
and why.

Any time we talk about Python I learn something.  Here are some
reflections on tuple-ness (which we've been discussing on this list
previously).  I'd never thought about it quite this way before Lloyd's
examples last night.  It may help you too - so, for what it's worth,
two examples (one in this message, another in a message to follow)...

_______________________________________________________________________
1.  Lloyd showed a snippet last night which at first seemed 
incomprehensible.  You may have figured it out right away, but it
stopped me cold at first.  Assume

>>> one_element_sequence = [42]

and assume that your code needs to set "a" to the first element of 
the sequence.  What I'd have coded (before last night's lesson) is

>>> a = one_element_sequence[0]
>>> a
42


What Lloyd showed us was

>>> [a] = one_element_sequence         # !
>>> a
42

Wow.  I've never coded brackets around something on the left side
before.  Really weird looking.  Was this some new Python operator
I'd never seen before?

  ***head scratching***

  ***more head scratching***

Well, turns out that it only looks weird.  I was able to do a 
series of reductions from forms that don't look weird.

If you do these snippets and explain what's happening at each 
step you'll probably agree.  And you may understand tuples and 
lists better.

For each snippet predict the result, then type it in.  Explain to
your own satisfaction what's different from each one to the next. 
(And  what's the same.)  Each one can be typed in in less than 10
seconds.

>>> one_element_sequence = [42]        # For later

>>> three_numbers = (1, 2, 3)
>>> three_numbers
(1, 2, 3)

>>> (b, c, d) = (1, 2, 3)
>>> b
1
>>> c
2
>>> d
3                                      # Printouts omitted from here on

>>> [e, f, g] = (1, 2, 3)
>>> e
>>> f
>>> g

>>> [h, i, j] = [1, 2, 3]
>>> h
>>> i
>>> j


>>> [k, m] = [1, 2]
>>> k
>>> m

>>> [n] = [1]
>>> n

----------------------------
And finally:
----------------------------

>>> one_element_sequence

>>> [p] = one_element_sequence
>>> p

----------------------------
Q.E.D.
----------------------------



More information about the Python-talk mailing list