Exceptions, assigning a tuple
Erik Max Francis
max at alcyone.com
Fri Nov 21 04:01:54 EST 2003
Derek Fountain wrote:
> And that's the final piece of the puzzle! I can now claim to
> understand it.
> Many thanks.
Sure thing. By the way, that little explanation is a hint at one of the
key insights to Python. A lot of features in Python work like that --
you don't have to provide dedicated support for a certain builtin
function to do its work (e.g., using the `tuple' function to make a
tuple out of an arbitrary object), you only need to make the object
conform to the relevant interface. In this case, it was the iteration
interface -- tuple will work with any object that can be iterated over.
So exactly the same property that allows you to write:
for x in myObject:
...
is precisely the same one that makes
tuple(myObject)
meaningful and do what you'd expect. For instance, to get a file-like
object, you don't actually need a literal file object (created with
`file'), you just need something that has the relevant methods (read,
readlines, write, writelines, flush, close, or a subset). Often these
sorts of interfaces are orthogonal, so that's why you saw that native
file objects actually can behave as iteratable objects as well -- all
they need to do is support read/write/flush/close and __getitem__ in the
expected way (start from 0, raise IndexError when you're done).
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ You're wasting time / Asking what if / You linger on too long
-- Chante Moore
More information about the Python-list
mailing list