Can't do a multiline assignment!

s0suk3 at s0suk3 at
Thu Apr 17 19:02:55 CEST 2008

On Apr 17, 11:46 am, Arnaud Delobelle <arno... at> wrote:
> Why not do something like:
> class RequestHeadersManager:
>     def __init__(self, string):
>         self._fields = {}
>         # Populate self.fields with fields defined in 'string'
>     def __getitem__(self, fieldname):
>         return self._fields.get(fieldname, None)
> This way you don't need to prebind all possible fields to None, and a
> field is accessible by its actual name, which should be easier to
> remember than an identifier derived from a field name.  Moreover you
> can more easily do some group manipulation of fields (e.g. print them
> all
>     def print_fields(self):
>         for name, value in self._fields.iteritems():
>             print "%s: %s" % (name, value)
> )

I do it with all the separate variables mainly for performance. If I
had the headers in a dict, I'd be looking up a string in a list of
strings (the keys of the dict) everytime I check for a header. Not
that that's going to take more that 0.1 seconds, but the program is
still small and simple. As it gets bigger, more features are gonna
slow things down.

More information about the Python-list mailing list