appending to a list via properties

Alex Martelli aleaxit at yahoo.com
Sat Feb 11 12:07:43 EST 2006


Carl Banks <invalidemail at aerojockey.com> wrote:
   ...
> >     class better_list (list):
> >         tail = property(None, list.append)
> 
> This is an impressive, spiffy little class.

Yes, nice use of property.

> growing_lists = foo,qux
> while some_condition:
>     for (s,x) in zip(growing_list,calculate_something()):
>         list.append(s,x)
> 
> No I don't really recommend it.

Why not?  Seems OK.  Maybe simplified to:

while some_condition:
    for alist, anitem in zip((foo, qux), calculate_something()):
        alist.append(anitem)

If you want to hoist for performance, you can hoist more:

appenders = foo.append, qux.append
while some_condition:
    for appender, anitem in zip(appenders, calculate_something()):
        appender(anitem)


Alex



More information about the Python-list mailing list