dict.update() useful or not?

"Martin v. Löwis" martin at v.loewis.de
Mon Aug 11 17:28:02 EDT 2008


>> def create_request(url, headers):
>>     headers.update(DEFAULT_HEADERS)
>>     req = urllib2.Request(url, None, headers)
>>     # ... 
>>     return req
>>
>> but of course this second example does the Wrong Thing, replacing 
>> explicit headers with default values.
> 
> There's a second code smell with that: even if it did what you want it 
> isn't nice to mutate the parameter passed in as headers. What if the caller 
> wants to reuse the headers again for another call? 

Just in case it isn't clear what the problem with that code is:
create_request is a function, ie. it returns a value. As such,
it shouldn't have any side effects. If it has side effects, it
should be considered a procedure, and return None.

Regards,
Martin



More information about the Python-list mailing list