bug or feature?

skip at pobox.com skip at pobox.com
Wed Oct 5 12:56:57 CEST 2005

    beza1e1> class A:
    beza1e1>    def __init__(self, lst=[]):
    beza1e1>       self.lst = lst

Lists are mutable and default args are only evaluated once, at function
definition.  If you want independent default args use:

    class A:
        def __init__(self, lst=None):
            if lst is None:
                lst = []
            self.lst = lst

The same scheme would work for other mutable types (dicts, sets, etc).

This same question gets asked once a month or so.  I'm sure this is in the
Python FAQ (check the website), but it was faster to reply than to look it


More information about the Python-list mailing list