Dynamic Lists, or...?

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Wed Jun 22 04:30:48 EDT 2005


On 13 Jun 2005 01:04:53 -0700, rumours say that "Raymond Hettinger"
<python at rcn.com> might have written:

># Professional driver on a closed course.
># Don't try this at home.
>
>data = """
>rose, 1, 500
>lilac, 1, 300
>lilly, 1, 400
>rose, 0, 100
>"""
>
>data = data.replace(', 1, ', ' += ')
>data = data.replace(', 0, ', ' -= ')
>
>class DefaultDict(dict):
>    def __getitem__(self, key):
>        return self.get(key, 0)
>
>d = DefaultDict()
>exec data in {}, d
>print d.items()

For kids trying this at home, note that it only works if the descriptor
is a valid Python identifier.
-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...



More information about the Python-list mailing list