Python's Mutable Default Problem 30

Posted by tottinger Fri, 23 May 2008 03:16:00 GMT

Today I was perusing a very fine python article all professional python programmers should read. I had to grin when I saw one of my favorite python quirks/flaws.

Python only evaluates default parameters when it defines a function, leading to surprising side effects (example from saifoo):
def function(item, stuff = []):
    stuff.append(item)
    print stuff

function(1)
# prints '[1]'

function(2)
# prints '[1,2]' !!!

The grin on my face was because I was bit by this one again last Tuesday. You just don’t want to make a mutable object a default parameter. The solution I used was close to the solution from saifoo:

Saifoo.net:
def function(item, stuff=None):
   if stuff is None:
      stuff = []
   stuff.append(item)
   print stuff
This, of course, gives the correct behavior because it creates a new empty list each time it is called without a second parameter.

My solution actually combines some advice contained earlier in the article with the solution given for this problem:

Mine
def function(item, stuff=None):
   stuff = stuff or []
   stuff.append(item)
   print stuff

It’s not necessarily prettier, and I struggle with whether it is more obvious (to a Python programmer) or not. I fear it may be clever (a word I only use in the pejorative sense), but it is also clean-looking to me.

The point remains, though, that the mutable default parameter quirk is an ugly corner worth avoiding, and I suspect that any code that purposefully exploits that behavior to populate a structure that persists across calls will run a very real risk of being misunderstood.

We all know what happens when the next programmer to touch a program misunderstands it.

Comments

Leave a response

  1. Avatar
    paddy3118 about 3 hours later:

    You get bit once, but have you seen anyone leave it in on purpose?

    - Paddy.

  2. Avatar
    Kevin about 5 hours later:

    So is that link up at the top supposed to point back to this article? Just curious :-)

    Kevin

    Ooops. Fixed now. Thanks. —tim
  3. Avatar
    Tim about 9 hours later:

    I’ve not used it on purpose. So far as I know, I’ve not seen it used on purpose. I’ve accidentally used it a few times. The tinker in me wants to try using it on purpose, and the professional refuses. Yet another case of ambivalence.

  4. Avatar
    John Roth about 13 hours later:

    Two comments. First, the or isn’t a hack. It’s a standard Python idiom, although it looks a bit strange to someone who doesn’t have a lot of Python experience.

    Second, having a mutable object as a parameter is definitely a gotcha for most Python programmers. The interesting thing is that it could be used as a way of getting a persistent variable into a method (local in some languages). However, I’ve never seen it used that way, and I’d be a bit leery of doing it myself except in programs that are part of a project where all the developers are comfortable with the idiom, and where the code isn’t expected to leave the project.

    John Roth

  5. Avatar
    matthew about 13 hours later:

    I wonder if this was a deliberate decision, to aid with recursion?

  6. Avatar
    yachris2112 4 days later:

    Once again, I have to point out that Lisp had this first :-). I remember (way) back in the day having this explained to me by none other than David Moon. For pythonistas, this is like seeing a noob post responded to by Guido.

    Anyway, Ruby has a similar idiom:

    somevar ||= default_val

    although the ‘or’ syntax would work as well. I think the ’||’ version is a bit more noticeable.

    matthew: almost certainly not.

  7. Avatar
    JustRanting 29 days later:

    I wonder how Python and professional fit together ;-)

  8. Avatar
    tim about 1 month later:

    @JustRanting:

    I guess about like this:

    http://www.google.com/search?q=python+professional&;ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

    Or this: http://www.google.com/trends?q=python%2C+professional&;ctab=0&hl=en

    Or maybe this: http://www.google.com/search?q=professional+python+job&;ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

    There is actually a lot more python out there than you think there is. And probably less than I hope. ;-)

  9. Avatar
    Olivier about 1 year later:

    I know it’s late, but I came across this page from Google, so in case someone else would look into it…

    Your own “fix” is incorrect, because an empty list evaluates to False in Python, so that if you try to use your function to append some item to an existing, yet empty, list, it won’t work.

  10. Avatar
    Tim Ottinger about 1 year later:

    Excellent catch. Should have checked that it was None instead. thank you for the update!

  11. Avatar
    dvla address about 1 year later:

    So far as I know, I’ve not seen it used on purpose. I’ve accidentally used it a few times. The tinker in me wants to try using it on purpose, and the professional refuses.

  12. Avatar
    gucci louis vuitton shoes about 1 year later:

    Welcome to Freshstyleshop, the hottest urban clothing site on the net! We offer great products from Gucci sneakers, prada sneakers, LV shoes, True Religion Jeans and many more! Our selection of products are always increasing for the fact that we have new items added weekly to our selection. All products on our site are already marked down 40-60% off retail price. Freshstyleshop also backs all its orders with a 110% satisfaction guarantee, making sure that our customers are left satisfied with the hottest products on the net.

  13. Avatar
    iphone fix about 1 year later:

    hope that the problem finally solved.

  14. Avatar
    disney restaurants about 1 year later:

    I’ve accidentally used it a few times and I wonder how Python and professional fit together.

  15. Avatar
    MTS File Converter about 1 year later:

    ah ha

  16. Avatar
    Bonakir about 1 year later:

    Yeah. this is cool

  17. Avatar
    Two?pice suit dress over 2 years later:

    Chic rose printing make you look exotic glamour for, But the soft snow is revealed your gentle temperament.

  18. Avatar
    shift over 2 years later:

    Cowherd skirt is hot girl never refuse, voted into the gradual change effect of hot pants to make you look more special, flanging style collocation with wrinkles around pocket design, add brown belts, and in a few ornament before fork, let you sexy metal buckles, feeling restoring ancient ways must try oh, beautiful eyebrow go shopping festival are handsome gas, absolutely super grab an eye.

  19. Avatar
    liding rode over 2 years later:

    OK, I got it. Thanks for your sharing. That is very interesting Smile I love reading and I am always searching for informative information like this.

  20. Avatar
    jingjia over 2 years later:

    MTS Converter can help you to convert mts files easily and quickly, and it is very easy to use. if you are in need, you can download it and experience by yourself.

    Main Features of MTS Converter

    • Convert AVC HD(.MTS) to all standard video formats like AVI, WMV, MPG, MOV, DVD, etc and various audio files like AAC, AC3, MP3, WAV, WMA, etc.
    • Support batch conversion, you can import more input mts files to the file playlist
    • Provide rich profiles, customize and save your profile for future use
    • Capture current pictures by click “snapshot” and save the current image to snapshot folder
    • Supports extract audio, pictures from video
    • Split one source file to several
  21. Avatar
    mattress over 2 years later:

    Thank you for the honest reviews. This helps me see deeper into how it actually works. Steam showers

  22. Avatar
    türk porno over 2 years later:

    thank you verygood

  23. Avatar
    cheap vps over 2 years later:

    Welcome to Freshstyleshop, the hottest urban clothing site on the net! We offer great products from Gucci sneakers, prada sneakers, LV shoes, True Religion Jeans and many more! Our selection of products are always increasing for the fact that we have new items added weekly to our selection. All products on our site are already marked down 40-60% off retail price. Freshstyleshop also backs all its orders with a 110% satisfaction guarantee, making sure that our customers are left satisfied with the hottest products on the net.cheap VPS

  24. Avatar
    valadarrel over 2 years later:

    Convert PDF files with high quality – transfer, backup, copy, import and export PDF files For many Apple users, to have a multi-performance support tool is a great thing. But even if you have all the iPad, iPhone, iPhone Touch of multi-tools, you will certainly encounter a lot of trouble. For example, some people may be using your iPad, but do not know how to use it to read e-books, some people will want to convert PDF to ePub , or transfer ePub files from your computer to the iPad, But do not know what tools to use, how to do it, some people still want to burn, copy, backup ePub files, but do not know how to enable them to maintain a high quality, something like this is really too much.

    But after you finish reading this article, you will not worry about these things, then you will know how to go for your iPad, iPhone, iPod Touch use professional tools to meet some of you common and uncommon problems. Now I will recommend you the best tools and the best way to do these things. You can use the powerful iPad Mate to rip your DVD, movie, music, and audio files to iPad, backup ePub files for your iPad and export ePub files from iPad, convert PDF files to ePub for you to watch eBooks like a PDF to ePub Converter . Also you can use the best iPad Manager for ePub to transfer your ePub files from your iPad and PC without losing them.

  25. Avatar
    gucci mens over 2 years later:

    Well , the view of the passage is totally correct gucci mens ,your details is really reasonable gucci belt for men and you guy give us valuable informative post, size11 gucci sneakers I totally agree the standpoint of upstairs. I often discount gucci jeans surfing on this forum classic gucci wallets when I m free and I find there are so much good information we can learn in this forum!

  26. Avatar
    iPhone battery replacement over 2 years later:

    Try Control Panel, System, Hardware, Device Manager, Display adapter, Properties, Update or Roll Back

  27. Avatar
    gucci wallet over 2 years later:

    gucci wallet, gucci wallets, mens gucci wallet, women gucci wallet.
    Price Guarantee, sale now.time limited.seize the chance.

  28. Avatar
    louis vuitton wallet over 2 years later:

    louis vuitton wallet, louis vuitton wallets, mens louis vuitton wallet, women louis vuitton wallet.
    Fashion, a general term for the style and custom prevalent at a given time, in its most common usage refers to costume or clothing style. The more technical term, costume, has become so linked in the public eye with the term “fashion” that the more general term “costume” has in popular use mostly been relegated to special senses like fancy dress or masquerade wear, while the term “fashion” means clothing generally, and the study of it.

  29. Avatar
    dupont lighter over 2 years later:

    dupont lighter, dupont lighters, st dupont lighter, s.t. dupont lighters. Great post,just as Great belts Sale time,you will enjoy the shopping time by belts Sale online store,and buy belts from belts Sale online store. Price Guarantee, sale now.time limited.seize the chance.

  30. Avatar
    LV belt over 2 years later:

    LV belt, LV belts, LV belts for men, LV Mens belts.
    Some actually seem like they are created with space creatures – not real women – in mind. But this fall, designers rolled out a multitude of great wearable looks.

Comments