Collections of non-arbitrary objects ?

Grant Edwards grante at visi.com
Thu Jun 21 20:30:36 EDT 2007


On 2007-06-21, Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> walterbyrd <walterbyrd at iname.com> writes:
>
>> Python seems to have a log of ways to do collections of
>> arbitrary objects: lists, tuples, dictionaries. But what if I
>> want a collection of non-arbitrary objects?

Lists never contain arbitrary objects, they contain only
objects that you put there.  The reason that other languages
have typed containers (e.g. array of type T) is that the
elements of the array don't know what type they are, so you've
got to limit what you put in there.  In python, all objects
know what type they are, so there's no point in labelling the
references to the objects with type info as well.

>> A list of records, or something like that?
>
> Then collect them in a non-arbitrary way.
>
> That's a flippant response, but I don't understand the
> question. What are you asking for that you don't already have?
> A list can contain a sequence of objects of the same type
> already. What are you expecting that a list does not provide?

I was also a bit baffled by the question.  The only things I
could think of are:

 1) a "container" that raised an exception if the type of a new
    item doesn't match the type of what's in it already.  I
    don't really see much benefit in that.  If you want a list
    to contain only objects of type T, then only put that type
    of objects in it.  

 2) an "array" that contains a large number of small things
   (e.g. integer or floating point numbers) that need to be
   stored with minimal overhead.  That's a useful thing, and
   there are several packages that do that -- numpy is the one
   generally recommended for new designs.

-- 
Grant Edwards                   grante             Yow! Are the STEWED PRUNES
                                  at               still in the HAIR DRYER?
                               visi.com            



More information about the Python-list mailing list