By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,403 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,403 IT Pros & Developers. It's quick & easy.

Using default a empty dictionary as a default value

P: n/a
Dear All,
I ran my code through pylint to clean it up and one of the (many) errors it
through up was
Dangerous default value {} as argument
Bascially, I have:

class NewClass:
def __init__(self, myDict={}):
for key, value in myDict:
print key, value

Obviously, this is not the way it should be done. Should I have a default
value of None for myDict and test for it? Or is there some other way?

Thanks

Colin

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
C Gillespie wrote:
Dear All,
I ran my code through pylint to clean it up and one of the (many) errors
it through up was
Dangerous default value {} as argument
Bascially, I have:

class NewClass:
def __init__(self, myDict={}):
for key, value in myDict:
print key, value

Obviously, this is not the way it should be done. Should I have a default
value of None for myDict and test for it? Or is there some other way?


Yes. That's the way to go.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
C Gillespie wrote:
Dear All,
I ran my code through pylint to clean it up and one of the (many) errors it
through up was
Dangerous default value {} as argument
Bascially, I have:

class NewClass:
def __init__(self, myDict={}):
for key, value in myDict:
print key, value

Obviously, this is not the way it should be done. Should I have a default
value of None for myDict and test for it? Or is there some other way?


Take a look at:

http://www.python.org/doc/2.4/tut/no...00000000000000

look for "Important warning"
HTH
--
////
(@ @)
----------------------------oOO----(_)----OOo--------------------------
<> Ojo por ojo y el mundo acabara ciego
/\ Alexis Roda - Universitat Rovira i Virgili - Reus, Tarragona (Spain)
-----------------------------------------------------------------------

Jul 18 '05 #3

P: n/a
C Gillespie wrote:
Dear All,
I ran my code through pylint to clean it up and one of the (many) errors it
through up was
Dangerous default value {} as argument
Bascially, I have:

class NewClass:
def __init__(self, myDict={}):
for key, value in myDict:
print key, value

Obviously, this is not the way it should be done. Should I have a default
value of None for myDict and test for it? Or is there some other way?


If you *know* you will not be modifying the contents
of myDict, and you *know* you will not be modifying
the function in the future to do so, then it's
completely safe as written...
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.