Can't do a multiline assignment!

Steve Holden steve at holdenweb.com
Thu Apr 17 16:16:48 EDT 2008


s0suk3 at gmail.com wrote:
> On Apr 17, 11:46 am, Arnaud Delobelle <arno... at googlemail.com> 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.

So basically you are optimizing for a performance problem you don't 
currently have. You want to stop that *straight* away. You are 
contorting your logic for absolutely no good reason.

Do what's easiest. Then, when you have that working correctly, speed it 
up (if you need to). Start with a dict. How do you know it won't be fast 
enough?

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list