Collections of non-arbitrary objects ?

Hamilton, William whamil1 at entergy.com
Tue Jun 26 11:25:22 EDT 2007


> From: walterbyrd
> 
> 
> Yes, but those languages also have the notion of structures that do
> not allow arbitrary collections. That is what I was wondering about
> when I started the thread. It's fine that python has four different
> ways of creating collections of arbitrary data types, but I thought it
> might be helpful if python had, at least, one way of a creating a
> collection of non-arbitrary data.

The thing is, in python all data is arbitrary.  The differences between
the data types float, int, string, etc. are unimportant.  What is
important is the _behavior_ of those types.  

If it is really necessary to check data when putting it into your
collection (user input, processing side-effects will cause damage,
etc.), test that it behaves the way you need it to behave rather than
requiring a specific type.


> It seems to me that tuple are essentially immutable lists. So why
> impose that immutability restriction on a data collection? Why not
> just have lists and not tuples? What can a tuple do that a list can
> not do? Why was python designed so that tuples could be used for
> dictionary indexes, but not lists? Could it be because imposing that
> restriction on a data collection insures a certain degree of
> integrity? My point is: sometimes imposing some restrictions can give
> a degree in built-in integrity. Maybe you don't need that integrity
> insurance, if you code carefully enough, but can you always count on
> that?

Tuples are more like structs in C.  They are collections of data in a
particular order.  Lists, on the other hand, are ordered collections of
data that all exhibit the same behavior.  You don't have to use them
like that, but those are the uses they were developed for as I
understand it.

Function arguments are the obvious example of a tuple.  The position of
an argument in the argument tuple defines how it is used in the
function; get the position wrong, and the function will act unexpectedly
and probably raise an exception.  Iterating over a set of function
arguments doesn't make much sense in the general case.

Lists, are generally intended to be iterated over.  You may take a list
of items that are addable, and add them.  You may take a list of
file-like objects and concatenate them into one file-like object.  (And,
note the "-like" part of that.  They don't have to be files, they just
have to be things that _behave_ like files for the file-like functions
you'll be using.)


> BTW: I'm not assuming that it will always be me running my own app.
> Sure, if an exception occureed while I was running my own app, I'd
> probably know what to do. But if somebody else (who - god forbid -
> didn't know python was running the app, things might be more
difficult.

So, what happens when this user tries to put something into your
restricted list that isn't allowed?  I expect they'll produce an
exception of some sort, and the app will crash if you don't handle that
exception.  Which is exactly what will happen if you don't restrict the
list data and it gets processed.  


--
-Bill Hamilton



More information about the Python-list mailing list