does Python support some kind of "casting"

Rich Harkins rich at worldsinfinite.com
Fri Mar 15 11:04:04 EST 2002


On Friday 15 March 2002 08:17 am, Peter Hansen wrote:
> Tino Lange wrote:
> > > Yes, that will make a copy. But copying a list is a pretty efficient
> > > operation, so no, it isn't really horribly inefficient. Have you tried
> > > it and identified this as a bottleneck? How long are your lists anyway?
> >
> > 3600 Elements each.
> >
> > No I didn't check if it's a bottleneck. But I immediately didn't feel
> > good when writing "return list(newlist)" (the list contains a list of
> > strings itself - so it's much stuff to copy)
>
> Get over it.  3600 elements is nothing, especially once you realize that
> the strings themselves are not duplicated, just the references.
>
> Anyway, the "immediately didn't feel good" thing is probably a feeling
> you'd best learn to ignore more. :-)  Only optimize when you have actually
> identified a bottleneck by profiling.  And a "bottleneck" is not something
> that runs "slow", but something that runs "too slow" which means you
> also need to have a specific requirement in mind before spending your
> valuable time optimizing.  Once I learned this it has made my programming
> life much easier.
>
> -Peter

Isn't there some additional overhead (above copying the underlying C array 
for the list) in having to bump each refcount of every object contained?  
This would appear on the surface to be not trivial when the number of 
referenced objects becomes large, especially considering that many of the 
object's ref counts may be swapped out or otherwise.  But your point about it 
is true, it is reasonably fast and I certainly can't think of a general 
solution that would be better.

OTOH, I do wonder if some form of list-based buffer() would be useful in 
these cases, so that you can take a slice of a list, bump the list's refcount 
without bumping the referenced objects (they're going to have one for the 
underlying list anyway), and proceed to process as necessary.  Granted, the 
usefulness in small list cases and cases where you want to modify the 
buffered thing would be quite limited, such a utility might be useful in 
large-scale list processing.  

Is anyone out there doing large scale list stuff in Python that would find 
this useful?  Has anyone else tried using buffer() to do similar slicing with 
strings?  I've had mixed results - sometimes it works like a charm, others 
some standard library won't accept the buffered() strings...

Just random thoughts,
Rich




More information about the Python-list mailing list