From: T on 7 Feb 2010 20:05 Ok, just looking for a sanity check here, or maybe something I'm missing. I have a class Test, for example: class Test: def __init__(self, param1, param2, param3): self.param1 = param1 self.param2 = param2 self.param3 = param3 Next, I have a dictionary mytest that contains instances of Test. If I want to modify one of the Test instances within my dictionary, I have to rewrite the entire entry, correct (since Python passes by value, not reference)? I.e. if I wish to change just param3 of an instance, I would have to do: def changevalue(): for key in mytest.keys(): currentparam = mytest[key] param1 = currentparam.param1 param2 = currentparam.param2 param3 = currentparam.param3 param3 = "newvalue" mytest[key] = Test(param1, param2, param3) If there's an easier way to accomplish this that I'm missing, that'd be great!
From: Chris Rebert on 7 Feb 2010 20:16 On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything(a)gmail.com> wrote: > Ok, just looking for a sanity check here, or maybe something I'm > missing. Â I have a class Test, for example: > > class Test: > Â Â def __init__(self, param1, param2, param3): > Â Â Â Â self.param1 = param1 > Â Â Â Â self.param2 = param2 > Â Â Â Â self.param3 = param3 > > Next, I have a dictionary mytest that contains instances of Test. Â If > I want to modify one of the Test instances within my dictionary, I > have to rewrite the entire entry, correct (since Python passes by > value, not reference)? Incorrect; Python uses neither. See http://effbot.org/zone/call-by-object.htm for a excellent explanation of what Python does use. > I.e. if I wish to change just param3 of an > instance, I would have to do: > > def changevalue(): > Â Â for key in mytest.keys(): > Â Â Â Â currentparam = mytest[key] > Â Â Â Â param1 = currentparam.param1 > Â Â Â Â param2 = currentparam.param2 > Â Â Â Â param3 = currentparam.param3 > Â Â Â Â param3 = "newvalue" > Â Â Â Â mytest[key] = Test(param1, param2, param3) > > If there's an easier way to accomplish this that I'm missing, that'd > be great! def changevalue(): for test in mytest.values(): test.param3 = "newvalue" Cheers, Chris -- http://blog.rebertia.com
From: T on 7 Feb 2010 20:24 On Feb 7, 8:16 pm, Chris Rebert <c...(a)rebertia.com> wrote: > On Sun, Feb 7, 2010 at 5:05 PM, T <misceveryth...(a)gmail.com> wrote: > > Ok, just looking for a sanity check here, or maybe something I'm > > missing. I have a class Test, for example: > > > class Test: > > def __init__(self, param1, param2, param3): > > self.param1 = param1 > > self.param2 = param2 > > self.param3 = param3 > > > Next, I have a dictionary mytest that contains instances of Test. If > > I want to modify one of the Test instances within my dictionary, I > > have to rewrite the entire entry, correct (since Python passes by > > value, not reference)? > > Incorrect; Python uses neither. Seehttp://effbot.org/zone/call-by-object.htmfor a excellent explanation > of what Python does use. > > > I.e. if I wish to change just param3 of an > > instance, I would have to do: > > > def changevalue(): > > for key in mytest.keys(): > > currentparam = mytest[key] > > param1 = currentparam.param1 > > param2 = currentparam.param2 > > param3 = currentparam.param3 > > param3 = "newvalue" > > mytest[key] = Test(param1, param2, param3) > > > If there's an easier way to accomplish this that I'm missing, that'd > > be great! > > def changevalue(): > for test in mytest.values(): > test.param3 = "newvalue" > > Cheers, > Chris > --http://blog.rebertia.com Thanks so much - this makes life a lot easier! And a great reference as well. Cheers, Doug
From: Steve Holden on 7 Feb 2010 20:31 T wrote: > Ok, just looking for a sanity check here, or maybe something I'm > missing. I have a class Test, for example: > > class Test: > def __init__(self, param1, param2, param3): > self.param1 = param1 > self.param2 = param2 > self.param3 = param3 > > Next, I have a dictionary mytest that contains instances of Test. If > I want to modify one of the Test instances within my dictionary, I > have to rewrite the entire entry, correct (since Python passes by > value, not reference)? I.e. if I wish to change just param3 of an > instance, I would have to do: > > def changevalue(): > for key in mytest.keys(): > currentparam = mytest[key] > param1 = currentparam.param1 > param2 = currentparam.param2 > param3 = currentparam.param3 > param3 = "newvalue" > mytest[key] = Test(param1, param2, param3) > > If there's an easier way to accomplish this that I'm missing, that'd > be great! def changevalue(): for key in mytest.keys(): mytest[key].param3 = "newvalue" regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/
From: Alf P. Steinbach on 7 Feb 2010 20:51
* Chris Rebert: > On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything(a)gmail.com> wrote: >> Ok, just looking for a sanity check here, or maybe something I'm >> missing. I have a class Test, for example: >> >> class Test: >> def __init__(self, param1, param2, param3): >> self.param1 = param1 >> self.param2 = param2 >> self.param3 = param3 >> >> Next, I have a dictionary mytest that contains instances of Test. If >> I want to modify one of the Test instances within my dictionary, I >> have to rewrite the entire entry, correct (since Python passes by >> value, not reference)? > > Incorrect; Python uses neither. See > http://effbot.org/zone/call-by-object.htm for a excellent explanation > of what Python does use. Hm. While most everything I've seen at effbot.org has been clear and to the point, that particular article reads like a ton of obfuscation. Python passes pointers by value, just as e.g. Java does. There, it needed just 10 words or so. :-) Or perhaps some more words to point out that in the Java language spec those reference values are called pointers, but that this terminology isn't (apparently) used for Python, and isn't even well known among Java programmers. But that's just one extra little para. One just has to be clear about exactly what it is that's passed by value. Not Python objects, but references (pointers) to them, the id(o) values. >> I.e. if I wish to change just param3 of an >> instance, I would have to do: >> >> def changevalue(): >> for key in mytest.keys(): >> currentparam = mytest[key] >> param1 = currentparam.param1 >> param2 = currentparam.param2 >> param3 = currentparam.param3 >> param3 = "newvalue" >> mytest[key] = Test(param1, param2, param3) >> >> If there's an easier way to accomplish this that I'm missing, that'd >> be great! > > def changevalue(): > for test in mytest.values(): > test.param3 = "newvalue" Cheers, - Alf |