Tuesday, July 5, 2011

Python: Beware of standard parameter values

I just spent an hour hunting for a strange bug in my Python code, and once I found it it made perfect sense (it often does when one is on that side of a bug hunt), but it was still obscure enough that I want to share it with you.

The bug occurred because of my (wrong) use of standard parameter values. Look at the following piece of code
class Foo(object):
def __init__(self, mylist = []):
self._mylist = mylist

def addSomethingToMyList(self, something):
self._mylist.append(something)

def __str__(self):
return str(self._mylist)

if __name__ == '__main__':
f = Foo()
f.addSomethingToMyList(42)
print "I just added 42 to f's mylist, now it contains", f
f2 = Foo()
f2.addSomethingToMyList(117)
print "I just added 117 to f2's mylist, now it contains", f2

Just by looking at it, one could expect that this would print:
I just added 42 to f's mylist, now it contains [42]
I just added 117 to f2's mylist, now it contains [117]

It doesn't though. What it prints is this:
I just added 42 to f's mylist, now it contains [42]
I just added 117 to f2's mylist, now it contains [42, 117]

Now why is that, you may ask? Well it has something to do with when standard parameter values are instantiated - which must be on class definition time. So if you use a standard parameter value that is a reference type (objects, list, dicts, sets, etc) it is that one instance that is passed on to all instances of your class.

The lesson therefore is: never use reference types in standard parameter values!

3 comments:

  1. Not long ago i went to the court to get an authorized replicate involving my very own Divorce Decree there was obviously a pair within wanting a wedding Certification, speculate his / her divorce, ten in the past was not recorded correctly, these were having many trouble!! Hiring the attorney is a big action therefore it may certainly be a terrifying "no turning back" action, however, if you're likely in which divorce is in your own potential go ahead and also take the decisive action to engage the right divorce attorney.
    virginia divorce attorney
    There is no doubt that the legal representative may have your own interest, yet he or she will not likely help to make unreasonable judgements depending on feelings. They are going to somewhat foundation decisions about fair payment as well as legal practices. If a person is going through a divorce with young children, these kinds of inner thoughts are even higher, obtaining another reason to engage children regulation expert. 4. Efficient- If a person is intending to accomplish their unique divorce, it will require a great deal of investigation, time and effort to be aware of your legal processing course of action, phrases and operations.

    ReplyDelete
  2. http://satkafoto.ru/event/31
    http://www.anarchycu.com/groups/60
    http://lidko.cz/blogs/post/2535
    http://code254.com/
    http://www.pirate.fluctis.com/piratenet/blogs/post/12
    http://leden.fanfaresintservatius.nl/photo/useralbums/admin
    http://hersimu.com/komunitasbahagia/groups/80
    https://www.theopenretreat.com/blogs/post/8878
    http://destinosexotico.com/blogs/post/895
    http://chopatti.com/groups/31

    ReplyDelete
  3. http://newtechgroupbd.com/?option=com_k2&view=itemlist&task=user&id=348419
    http://nicaragualibre.info/?option=com_k2&view=itemlist&task=user&id=40939
    http://nickelparsian.ir/?option=com_k2&view=itemlist&task=user&id=175819
    http://nivaldoca.com/?option=com_k2&view=itemlist&task=user&id=119938
    http://nmcinteriordesign.co.za/?option=com_k2&view=itemlist&task=user&id=1191360
    http://nobreakmonofasico.com.br/?option=com_k2&view=itemlist&task=user&id=220189
    http://notariat-public-bucuresti.ro/index.php/component/k2/itemlist/user/273057
    http://novadentalanesthesia.com/index.php/component/k2/itemlist/user/627837
    http://ntaccul.cm/index.php/en/component/k2/itemlist/user/5473
    http://ny.latambschool.com/?option=com_k2&view=itemlist&task=user&id=447756

    ReplyDelete