Enumeration of strings and export of the constants

Ian ian.g.kelly at gmail.com
Wed Nov 10 18:30:01 EST 2010


On Nov 10, 6:12 am, lnenov <lne... at mm-sol.com> wrote:
> Is there a better and more common way to do this?

from itertools import count, izip

class APINamespace(object):

    def __init__(self):
        self._named_values = []

    def enumerate(self, names, start=0, step=1):
        self._named_values.extend(izip(names, count(start, step)))

    def import_to(self, destination):
        for name, number in self._named_values:
            setattr(destination, name, number)

Note the "step" parameter of itertools.count requires Python 2.7 or
3.1.

> And can line 6 be replaced by something less evil.

Yes, by putting the names to be imported into their own container as
I've done above, instead of polluting the class dictionary with them.

Cheers,
Ian



More information about the Python-list mailing list