Can't do a multiline assignment!
J. Cliff Dyer
jcd at sdf.lonestar.org
Thu Apr 17 14:55:00 EDT 2008
On Thu, 2008-04-17 at 13:53 -0400, Steve Holden wrote:
> Gary Herron wrote:
> > s0suk3 at gmail.com wrote:
> >> On Apr 17, 10:54 am, colas.fran... at gmail.com wrote:
> >>
> >>> On 17 avr, 17:40, s0s... at gmail.com wrote:
> >>>
> >>> Out of sheer curiosity, why do you need thirty (hand-specified and
> >>> dutifully commented) names to the same constant object if you know
> >>> there will always be only one object?
> >>>
> >> I'm building a web server. The many variables are names of header
> >> fields. One part of the code looks like this (or at least I'd like it
> >> to):
> >>
> >> class RequestHeadersManager:
> >>
> >> # General header fields
> >> Cache_Control = \
> >> Connection = \
> >> Date = \
> >> Pragma = \
> >> Trailer = \
> >> Transfer_Encoding = \
> >> Upgrade = \
> >> Via = \
> >> Warning = \
> >>
> >> # Request header fields
> >> Accept = \
> >> Accept_Charset = \
> >> Accept_Encoding = \
> >> Accept_Language = \
> >> Authorization = \
> >> ...
> >>
> >
> > But. *What's the point* of doing it this way. I see 14 variables
> > being assigned a value, but I don't see the value, they are getting.
> > Reading this bit if code provides no useful information unless I'm
> > willing to scan down the file until I find the end of this mess. And in
> > that scanning I have to make sure I don't miss the one single line that
> > does not end in a backslash. (Your ellipsis conveniently left out the
> > *one* important line needed to understand what this code is doing, but
> > even if you had included it, I'd have to scan *all* lines to understand
> > what a single value is being assigned.
> >
> > There is *no way* you can argue that code is clearer than this:
> >
> > # General header fields
> > Cache_Control = None
> > Connection = None
> > Date = None
> > Pragma = None
> > ...
> >
> Thank you, you saved me from making that point. It doesn't even seem
> like there's a need for each header to reference the same value (though
> in this case they will, precisely because there is only one None object).
>
> regards
> Steve
Another possibility is to assign to a dict using a loop, if typing None
over and over again is so onerous.
options = ['cache_control',
'connection',
'date',
'pragma']
params = {}
for option in options:
params[option] = None
And of course you could substitute your choice of appropriate __dict__
for params, if you want to access the options as free-standing objects.
Cheers,
Cliff
More information about the Python-list
mailing list