Can't do a multiline assignment!

Paul Hankin paul.hankin at gmail.com
Thu Apr 17 14:19:48 EDT 2008


On Apr 17, 5:56 pm, s0s... at gmail.com wrote:
> class RequestHeadersManager:
>     ...
>     def __init__(self, headers, linesep):
>         headersDict = parse_headers(headers, linesep)
>
>         for header in headersDict.keys():
>         ...[lots of code]

Your code is pretty much equivalent to this (untested).

class RequestHeadersManager(object):
    def __getattr__(self, field):
        if not hasattr(self, field):
            return None
    def __init__(self, headers, linesep):
        for header, value in parse_headers(headers,
linesep).iteritems():
            header = '_'.join(x.capitalize() for x in
header.split('-'))
            setattr(self, header, value)

You may want to add some error checking though!

--
Paul Hankin



More information about the Python-list mailing list