except clause syntax question

Devin Jeanpierre jeanpierreda at gmail.com
Tue Jan 31 11:38:44 EST 2012


On Tue, Jan 31, 2012 at 11:23 AM, Charles Yeomans
<charles at declaresub.com> wrote:
>
> On Jan 31, 2012, at 9:51 AM, Steven D'Aprano wrote:
>
>> On Tue, 31 Jan 2012 08:57:31 -0500, Charles Yeomans wrote:
>>
>>> I don't think of a tuple as a container, and I don't think it a
>>> misunderstanding on my part to think this.
>>
>> Well, it is a misunderstanding, because tuples ARE containers. You might
>> as well say "I don't think of boxes as containers". What exactly are they
>> if not containers?
>
>
> Tuple is a heterogenous datatype that allows one to define objects ad hoc. That is to say, a tuple represents a single thing distinct from its components.  For example, suppose you need to represent a location in text by line number and offset within a line.  A tuple object makes it easy to do so without writing a class having no methods other than a constructor.  Here, the components, a line number and an offset, define a new object distinct from the pieces.
>
> One can certainly view a tuple as a list, just as one can view a string as a list of characters, and sometimes that's useful; the Python dictum "there should only be one way to do it" doesn't imply that there is only one way to think of it.
>
> Nor am I the only person who sees such a distinction between tuple and list.

Perhaps it'd be useful to look at how the Python language reference
defines containers?

Quote:

Some objects contain references to other objects; these are called
containers. Examples of containers are tuples, lists and dictionaries.
The references are part of a container’s value. In most cases, when we
talk about the value of a container, we imply the values, not the
identities of the contained objects; however, when we talk about the
mutability of a container, only the identities of the immediately
contained objects are implied. So, if an immutable container (like a
tuple) contains a reference to a mutable object, its value changes if
that mutable object is changed.

End quote.
(Offtopic: How do I do an external block quote appropriately in an email?)

Tuples are most certainly containers, precisely _because_ they're an
ad-hoc way to define objects, where the only purpose of the object is
to contain the values inside the tuple.

But these are just words and it's beside the point. We should talk of
things as if the word "container" didn't matter, because I don't think
that's what you meant, but neither do I want to put words in your
mouth.

My interpretation is that you see tuples as an object where you can
get meaningfully things by field, rather than just grab arbitrarily
from a bag of things. This isn't the only way they are used, see the
except statement (hee) and their use as keys in dictionaries. But it
is true that their immutable length makes them very well suited to the
task of representing product types.

-- Devin



More information about the Python-list mailing list