Optional arguments are evil (II)

Time ago I wrote about the perils of optional parameters in Python. Now I've found another reason why they are dangerous, although we still need them.

What we have realized today is that, default values for optional arguments might share its state among multiple calls. You might have a Borg pattern where you don't want it, and that is dangerous:

  2. def my_func_with_optional_list(arg1, arg2=[]):
  3. arg2.append(arg1)
  4. return arg2
  6. def my_func_with_optional_dict(arg1, arg2={}):
  7. arg2[arg1] = arg1
  8. return arg2
  10. if __name__ == '__main__':
  11. print my_func_with_optional_list(1)
  12. print my_func_with_optional_list(1)
  13. print my_func_with_optional_dict('carlos')
  14. print my_func_with_optional_dict('oscar')

Can you see anything that could keep the state between calls? I can't, however, this is the output:

  2. [1]
  3. [1, 1]
  4. {'carlos': 'carlos'}
  5. {'oscar': 'oscar', 'carlos': 'carlos'}

What happen? My understanding is that, because the list and dictionary objects are mutable and they are defined along with the function itself, python creates them in the global namespace.

We are now reviewing again every function we've written searching for this pattern.

  • http://www.sharpbites.com alberto

    Hi Carlos.

    This may help you clarify it:
    Default parameter values are evaluated when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function.

    Extracted from: http://docs.python.org/release/2.6.5/reference/compound_stmts.html#function-definitions

  • http://carlosble.com Carlos Ble

    Thanks Alberto, we are doing that, using None as default params 🙂
    Take care

  • http://abelmuino.com Abel Muiño

    My perception is that the problem is not with default values, but with using an argument as a return value. If you want a method to modify something, you need to give it that something…

    Default values (and the surprising handling Python makes of them) only “mask” the original problem here.

    BTW, I have just tested and ruby does not have this behavior, so the title should be (at least) “Default arguments ON PYTHON are evil” 🙂

  • http://www.webwire.com/ViewPressRel.asp?aId=136705 End Of Day Trading

    Hey! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no back up. Do you have any solutions to prevent hackers?

  • http://www.ringworm-cure.com/ ringworm remedies

    What are some great ideas on teaching creative writing to kids?

  • http://community.atom.com/Post/Optimum-Offer-on-Nikon-Coolpix-S8100/03EFBFFFF02536FAD00080177A743 nikon s8100

    This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!

  • http://exceltriathlon.com.previewmysite.com/phpBB2/profile.php?mode=viewprofile&u=1016265 Pricilla Sloter

    Thank you for your blog post. Manley and I have been saving for our new book on this subject matter and your article has made all of us to save the money. Your opinions really solved all our questions. In fact, a lot more than what we had acknowledged in advance of the time we found your amazing blog. My spouse and i no longer nurture doubts and also a troubled mind because you totally attended to our needs in this article. Thanks

  • http://doit.tweeteigo.jp/blogs/6086/127176/all-the-unwanted-lady8217 ugg outlet store locations in pa hit bg

    strongzz hi!,I like your writing so much! share we communicate more about your article on AOL? I need an expert on this area to solve my problem. Maybe that’s you! Looking forward to see you.

  • http://the-app-market.net/ android apps

    Thanks for your recommendations on this blog. Just one thing I would wish to say is that purchasing electronics items on the Internet is nothing new. Actually, in the past ten years alone, the marketplace for online gadgets has grown noticeably. Today, you’ll find practically any kind of electronic tool and other gadgets on the Internet, from cameras and also camcorders to computer components and game playing consoles.

  • http://drewgiles1210.posterous.com/cold-calling-techniques-that-really-work Cold Calling

    Hi there, I discovered your website via Google even as looking for a similar topic, your site came up, it seems to be great. I’ve bookmarked it in my google bookmarks.