Modifying Class Object

Steve Holden steve at holdenweb.com
Sun Feb 7 20:31:31 EST 2010


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/




More information about the Python-list mailing list