Created
April 17, 2012 05:20
-
-
Save idan/2403662 to your computer and use it in GitHub Desktop.
Parameter Usage Style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Which of these | |
def __init__(self, uri, http_method=u'GET', body={}, headers={}): | |
# do stuff with body and headers directly | |
headers['Content-Type'] = u'x-www-url-formencoded' | |
# etc... | |
# VERSUS # | |
def __init__(self, uri, http_method=u'GET', body=None, headers=None): | |
body = body or {} | |
headers = headers or {} |
Apparently, the former is super-dangerous because dicts are mutable. (See: https://gist.github.com/2403699)
Also, I'm retarded.
i think
def __init__(self, uri, http_method="GET", body=None, headers=None):
is preferred see http://effbot.org/zone/default-values.htm
You should do what @dstufft recommends :)
I prefer the shorter version of @dstufft
def __init__(self, uri, http_method="GET", body=None, headers=None):
body = body or {}
headers = headers or {}
@tebeka it might be simpler, but its useless extra cost, and is implicit behavior of overwriting the structure that you passed in
@dcramer I don't follow. If you passed an empty structure, then a new empty one will be created which is not a big issue. The idea is not to mutate default arguments.
If you really care, you can probably use body = {} if body is None else body
if you want to check for None explicitly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I always prefer:
But It's kinda verbose.