appending to a list via properties
Carl Banks
invalidemail at aerojockey.com
Sat Feb 11 10:11:39 EST 2006
Lonnie Princehouse wrote:
> Here's a curious hack I want to put up for discussion. I'm thinking of
> writing a PEP for it.
A minor library change wouldn' t need a PEP.
> Observation
> -----------------
> I found myself using this construct for assembling multiple lists:
>
> foo = []
> qux = []
>
> while some_condition:
> a, b = calculate_something()
> foo.append(a)
> qux.append(b)
>
> Not elegant!
I don't agree; however:
> class better_list (list):
> tail = property(None, list.append)
This is an impressive, spiffy little class.
> Well?
I suspect it's too sneaky and not commonly useful enough to get serious
consideration for the standard library. But definitely submit it to
Python Cookbook:
http://aspn.activestate.com/ASPN/Python/Cookbook/
Carl Banks
P.S. to get rid of temporary variables while using regular lists:
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.
More information about the Python-list
mailing list