Using default a empty dictionary as a default value

Discussion in 'Python' started by C Gillespie, Mar 22, 2005.

  1. C Gillespie

    C Gillespie Guest

    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
    C Gillespie, Mar 22, 2005
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Mar 22, 2005
    #2
    1. Advertising

  3. 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/node6.html#SECTION006710000000000000000

    look for "Important warning"


    HTH
    --
    ////
    (@ @)
    ----------------------------oOO----(_)----OOo--------------------------
    <> Ojo por ojo y el mundo acabara ciego
    /\ Alexis Roda - Universitat Rovira i Virgili - Reus, Tarragona (Spain)
    -----------------------------------------------------------------------
    SER.RI-TIC-Alexis Roda, Mar 22, 2005
    #3
  4. C Gillespie

    Peter Hansen Guest

    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...
    Peter Hansen, Mar 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Ahlers
    Replies:
    1
    Views:
    5,625
    Martin Honnen
    Jul 12, 2004
  2. Ilias Lazaridis
    Replies:
    6
    Views:
    416
    Ilias Lazaridis
    Feb 21, 2006
  3. james_027
    Replies:
    1
    Views:
    300
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Navkirat Singh
    Replies:
    6
    Views:
    2,936
    Navkirat Singh
    Jul 29, 2010
  5. Chris Rebert
    Replies:
    0
    Views:
    499
    Chris Rebert
    Jul 29, 2010
Loading...

Share This Page