On Apr 17, 5:19 pm, [EMAIL PROTECTED] wrote: > On Apr 17, 10:54 am, [EMAIL PROTECTED] wrote: > > > On 17 avr, 17:40, [EMAIL PROTECTED] 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 = \ > ... > > Etc etc etc. At the end they'll all be assign to None. Then, when > initialized, __init__() will the the string of headers, parse them, > and use those variables shown above to assign to the header values. Of > course a normal request won't include all of those headers, so the > others will remain None. That's what I want.
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) ) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list