List Behavior when inserting new items

Paul McGuire ptmcg at austin.rr.com
Mon Jan 29 21:25:02 CET 2007


> py>    def __init__(self, arg = []):
> py>        self.__list = arg

Please don't perpetuate this bad habit!!!  "arg=[]" is evaluated at 
compile time, not runtime, and will give all default-inited llists the 
same underlying list.

The correct idiom is:

    def __init__(self, arg = None):
        if arg is not None:
            self.__list = arg
        else:
            self.__list = []

-- Paul




More information about the Python-list mailing list