On 8/16/05, Martijn Brouwer <[EMAIL PROTECTED]> wrote: > Well, I guess making a deep copy of an object is important in many case. > Just try this: > A=[] > B=A > A.append("foo") > print B > > Guess the output. It is: > ['foo']
I remember thinking that this behavior was odd when I learned Java many years ago. (Java shares this behavior, and I learned it before Python. In fact, I think that many OO languages behave this way - JavaScript, C#, and so on.) But these days, it's 2nd nature. I also thought that it might be dangerous, but now I know that it hardly ever is. (When is it dangerous? Well, if you are trying to build an immutable class, you have to be careful not to pass out references to mutable attributes.) > In many cases this is undesirable, so you need to make a deep copy using > something as B=copy.deepcopy(A). This is where we differ. I know that it's *occasionally* useful to be able to copy or deep-copy an object - that's why the copy module is there. But I find that it's not "many cases" - it's actually rather rare. Shallow copies of dictionaries and (especially) lists are a little less rare, but you can make those without the copy module: new_list = list(old_list) # or old_list[:] new_dict = dict(old_dict) # or old_dict.copy() -- Cheers, Simon B, [EMAIL PROTECTED], http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/python-list