[Fwd: Re: managed lists?]

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon May 21 17:02:52 EDT 2007


(snip - I suppose I'm answering to Jorgen, but being another idiot 
myself I may be wrong... anyway:)

> Thanks for the answer. I did make something myself after i could not
> find anything, just because it was fun to do :-) I did saw array but
> it was not for object, only for small types like int, char, 

Everything in Python is an object. Including integers. And there's no 
'char' type in Python.

> 
> The list I created uses a strong type check to make sure all objects
> in the list are of the same type, like
> 
> ol = objlist.ObjList(class_name = SomeClass)
> 
> Now only classes that are an instance of SomeClass are allowed in the
> array. ObjList mimics a normal list, it can index, iterate, find,
> delete, append items so it was basically a drop-in replacement for my
> _list = [] solution

The problem with this kind of "solutions" is that it creates more 
problems than anything else.

What you really want is a list objects that share a common interface, 
not a list of objects that are instances of a same class. As a simple 
example, let's say you need a list of file-like objects. Or to be more 
accurate, a list of objects that have a 'write' method that's compatible 
with file.write(). So StringIO instances should be allowed, but your 
code will reject them. You may have a perfectly valid reason to use 
StringIO instances instead of files in some situations, but with your 
(ill-named) 'ObjectList' stuff it won't be possible. For no good reason.



More information about the Python-list mailing list