<div class="gmail_quote">On Mon, Oct 5, 2009 at 3:41 PM, David Perlman <span dir="ltr"><<a href="mailto:dperlman@wisc.edu">dperlman@wisc.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
OK, I thought I had this one fixed but it was weirder than I thought. I think I understand what's going on, but I wanted to check with the experts here.<br>
<br>
I have the following class definition, which does not subclass anything:<br>
<br>
class oneStim:<div class="im"><br>
def __init__(self, time, mods=[], dur=None, format='%1.2f'):<br>
self.time=time<br>
self.mods=mods<br></div></blockquote><div><br></div><div>I think this is where the fun occurs (AFAIK, I'm not an expert on how the python lists/namespaces work by any means)</div><div><br></div><div>You are familiar with the following concept/behavior, correct?</div>
<div><br></div><div>def f1(mylist):</div><div> mylist.append(3)</div><div><br></div><div>a = [1,2]</div><div><div>>>> f1(a)</div><div>>>> a</div><div>[1, 2, 3]</div><div><br></div><div>because python doesn't actually pass a copy, just the reference of the list. So I'm betting that when you say</div>
<div><br></div><div>self.mods = mods </div><div><br></div><div>the second time you call the function you're creating a reference to the self.mods that's in the oneStim object namespace.</div><div><br></div><div>Ahhh... After some experimentation, I think I see what's going on.</div>
<div><br></div><div>class A:</div><div> def __init__(self, mylist = []):</div><div> self.mylist = mylist</div><div><br></div><div>a = A()</div><div>b = A()</div><div><br></div><div><div>In [69]: a.__init__.im_func</div>
<div>Out[69]: <function __init__ at 0x95888ec></div><div><br></div><div>In [70]: A.__init__.im_func</div><div>Out[70]: <function __init__ at 0x95888ec></div><div><div><br></div><div>In [71]: b.__init__.im_func</div>
<div>Out[71]: <function __init__ at 0x95888ec></div></div><div><br></div><div>The function is at the same address for the parent class and each instance of the class, so I think the reference it creates is to the list in the same location as well. </div>
<div>Ah... some enlightenment has struck with what bob posted.</div><div><br></div><div>The "initial value" of that function is the /exact same/ each time the function is called. When you pass a parameter to the function it will be re-evaluated, but until then the pre-stored reference is used.</div>
<div><br></div><div>I'm not sure if that's terribly clear or makes much sense, it's a bit of a difficult concept to wrap my head around, but I *think* I've got it.</div><div><br></div><div>Hopefully you can get it, too,</div>
<div>Wayne</div></div><div><br></div><div><br></div></div></div>