unexpected class behaviour

kaer kaerbuhez at gmail.com
Sun Sep 7 13:16:24 CEST 2008


On 7 sep, 12:40, Fredrik Lundh <fred... at pythonware.com> wrote:
> Jan Schäfer wrote:
> > can anyone explain the behaviour of the following code sniplet:
>
> well, it *is* explained in the tutorial, the language reference, and the
> FAQ, so yes, it can be explained ;-)
>
> for more information, see this page:
>
>      http://effbot.org/zone/default-values.htm
>
> </F>

Well, you may want replace the last line by:
    print ivar, obj, obj.varlist, id(obj.varlist)

To have another behavior, you may want replace the 3 first lines by:
class Base(object):
    def __init__( self, lst=None ):
        if lst is None: lst=[]
        self.varlist = lst

Enjoy Python !



More information about the Python-list mailing list