|
|||
|
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! |
|
|
||||
|
||||
|
|
|
|||
|
On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything@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 |
|
|||
|
On Feb 7, 8:16*pm, Chris Rebert <c...@rebertia.com> wrote:
> On Sun, Feb 7, 2010 at 5:05 PM, T <misceveryth...@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 |
|
|||
|
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/ |
|
|||
|
* Chris Rebert:
> On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything@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 |
|
|||
|
Alf P. Steinbach wrote:
> * Chris Rebert: >> On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything@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. > A reference is not the same as a pointer. A pointer tells you where something is; a reference doesn't. |
|
|||
|
* MRAB:
> Alf P. Steinbach wrote: >> * Chris Rebert: >>> On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything@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. >> > A reference is not the same as a pointer. Depends on your choice terminology. I referred to the Java (language spec) terminology to make it clear. > A pointer tells you where something is; a reference doesn't. Sorry, I don't know of any relevant terminology where that is the case. Cheers & hth., - Alf |
|
|||
|
Alf P. Steinbach wrote:
> * MRAB: >> Alf P. Steinbach wrote: >>> * Chris Rebert: >>>> On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything@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. >>> >> A reference is not the same as a pointer. > > Depends on your choice terminology. I referred to the Java (language > spec) terminology to make it clear. > > >> A pointer tells you where something is; a reference doesn't. > > Sorry, I don't know of any relevant terminology where that is the case. > Alf: This topic was discussed at great, nay interminable, length about a year ago. I'd appreciate it if you would search the archives and read what was said then rather than hashing the whole topic over again to nobody's real advantage. 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/ |
|
|||
|
On Sun, 07 Feb 2010 22:03:06 -0500, Steve Holden wrote:
> Alf: > > This topic was discussed at great, nay interminable, length about a year > ago. I'd appreciate it if you would search the archives and read what > was said then rather than hashing the whole topic over again to nobody's > real advantage. Curse you Steve, I had just come up with a brilliant rebuttal of Alf's position. It was sheer genius, the sort of thing that would have James Gosling weeping with envy. Oh well, into the bitbucket it goes... -- Steven |
|
|||
|
"Alf P. Steinbach" <al...@start.no> wrote:
> 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. Must. Resist. Ad hominem. > Python passes pointers by value, just as e.g. Java does. > > There, it needed just 10 words or so. :-) 10 words _plus_ an understanding of Java. Do you really think its appropriate to discuss Python's behaviour purely in terms of other languages? Further, you've managed to define Python's behaviour as being somehow _both_ of the major evaluation strategies - calling a reference by value - so you're asking people to understand two totally irrelevant models just to avoid describing one in its own terms. Rather than arguing about whether you have a 'value' or a 'reference', it's a lot easier to explain that you're passing mutable & immutable objects around. The behaviour is thus defined in terms of the object and _not_ in the calling model, and is far more consistent with object references throughout the language. It also doesn't require reference to other languages simply to define Python's model in terms of what it isn't. |
|
|||
|
* Steve Holden:
> Alf P. Steinbach wrote: >> * MRAB: >>> Alf P. Steinbach wrote: >>>> * Chris Rebert: >>>>> On Sun, Feb 7, 2010 at 5:05 PM, T <misceverything@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. >>>> >>> A reference is not the same as a pointer. >> Depends on your choice terminology. I referred to the Java (language >> spec) terminology to make it clear. >> >> >>> A pointer tells you where something is; a reference doesn't. >> Sorry, I don't know of any relevant terminology where that is the case. >> > Alf: > > This topic was discussed at great, nay interminable, length about a year > ago. I'd appreciate it if you would search the archives and read what > was said then rather than hashing the whole topic over again to nobody's > real advantage. Well that's my point, and thanks for backing me up on that :-): it's very simple, and as demonstrated can be expressed in 10 words or less (plus perhaps a terminology reference, as I did above), so all that discussion and in particular the lengthy article at effbot serves as obfuscation and nothing else. By the way, most every programming language has some corner like that, something that is utterly simple but somehow has some kind of obfuscation-meme attached. In C++ it's "call" and "constructor". It doesn't help that the language's standard lays down the law on it, it doesn't help that the language's creator has laid down the law, it doesn't help that it's utterly and completely simple. Somehow newbies and even some experienced people manage to create their own terminological nightmare and drawing conclusions about reality from that misguided obfuscated view, and then discussing it up and down and sideways. Cheers & hth., - Alf |
|
|||
|
On Mon, 08 Feb 2010 02:51:05 +0100, Alf P. Steinbach wrote:
> Python passes pointers by value, just as e.g. Java does. How do I get a pointer in pure Python code (no ctypes)? I tried both Pascal and C syntax (^x and *x), but both give syntax errors. For that matter, how do I get a pointer in Java code? If Python doesn't have pointers, then why are you talking about Python passing pointers? It's a vacuous truth, like saying that Python passes dinosaurs by name. -- Steven |
|
|||
|
Oops, this one was my fault - the object I was having the issues with
was actually a shelve file, not a dictionary..so just re-assigning the variable isn't working, but re-writing the object to the shelve file does. So in this case, is there any way to just change a single value, or am I stuck rewriting the entry? |
|
|||
|
On Mon, 08 Feb 2010 03:21:11 +0100, Alf P. Steinbach wrote:
>> A pointer tells you where something is; a reference doesn't. > > Sorry, I don't know of any relevant terminology where that is the case. Taken from Wikipedia: "A pointer is a simple, less abstracted implementation of the more abstracted reference data type (although it is not as directly usable as a C++ reference)." http://en.wikipedia.org/wiki/Pointer_(computing) In other words, a pointer is a specific type of reference. A reference in turn is an opaque but low-level data type which "refers to" in some way to the data you actually care about. (C++ has a concrete reference type, which is not to be confused with abstract references.) http://en.wikipedia.org/wiki/Referen...mputer_science) Unless otherwise stated, references are opaque and coders need not care how the reference mechanism is implemented, see e.g.: http://www.cocoabuilder.com/archive/...reference.html In Python you don't use references directly, there is no reference type or object. You can simulate the semantics of references (but not pointers) by putting your object in a list and passing the list around. -- Steven |
|
|||
|
* Steven D'Aprano:
> On Mon, 08 Feb 2010 02:51:05 +0100, Alf P. Steinbach wrote: > >> Python passes pointers by value, just as e.g. Java does. > > How do I get a pointer in pure Python code (no ctypes)? I tried both > Pascal and C syntax (^x and *x), but both give syntax errors. Well, I don't believe that you tried that. :-) From one point of view it's extremely easy: just create some object, even just type 42 as an integer literal, and you can apply all of Python's pointer operations to the result -- which isn't much, basically just checking for pointer equality via 'is' or applying 'id' to get a value that represents the pointer uniquely, or copying the pointer via assignment or parameter passing. Whether you can obtain the bits of the internal pointer value, represented as e.g. an int, formally depends on the Python implementation. In CPython the 'id' function provides the internal pointer value as an int. I.e., with CPython you can do def foo( o ): print( id( o ) ) # Shows the pointer value in decimal. whatever = 42 print( id( whatever ) ) # Shows the pointer value in decimal. foo( whatever ) # Shows the exact *same* pointer value. which at a slightly higher level of abstraction works just as well with any conceivable Python implementation, although you have no formal guarantee that the conceptual "pointer" values are actually the internally memory addresses. But, in CPython they are, and you run into them all the time, for example (where the "at" tells you that it's a memory location specification, a pointer), >>> import turtle >>> turtle.forward <function forward at 0x00DB7D20> >>> >>> id( turtle.forward ) 14384416 >>> hex( id( turtle.forward ) ) '0xdb7d20' >>> _ > For that matter, how do I get a pointer in Java code? As with Python, from one point of view it's extremely easy, just 'new' some object, and then you can apply all of Java's pure pointer operations to the result -- which isn't much, basically just checking for pointer equality and copying a pointer via assignment or parameter passing. In Java it's a pointer by definition, namely the language spec's definition. From another point of view, getting at the internal representation of the pointer is a bit harder in Java than in Python, at least as far as I know. It may not be practically possible. Disclaimer: I'm not a Java expert, and haven't used Java for years, and it just might be possible by using JNI (Java Native Interface), but that requires you to write a dynamic library in e.g. C or C++, and as I recall Java just creates a kind of fixed reference for the duration of a JNI call so the JNI side of things may not tell you anything about the Java side of things before or after the call. But if it helps, just to learn about pointers in various languages you -- or rather, some other reader, because I suspect that you know this already! :-) -- might look at <url: http://cslibrary.stanford.edu/106/>. It contains a simple pointers example expressed in four different languages, namely C, C++, Pascal and Java, in particular comparing C and Java. > If Python doesn't have pointers, then why are you talking about Python > passing pointers? It's a vacuous truth, like saying that Python passes > dinosaurs by name. See above. Cheers & hth., - Alf |
|
|
![]() |
| Popular Tags in the Forum |
| class, modifying, object |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Use of extend <module> from within a Class | Mike Papper | Newsgroup comp.lang.ruby | 6 | 12-08-2009 01:02 AM |
| Any official name for Ruby's class which makes "class methods"? | Hunt Jon | Newsgroup comp.lang.ruby | 17 | 11-19-2009 11:49 PM |
| What's the different between the function and method? | Yongwei Xing | Newsgroup comp.lang.lisp | 7 | 10-11-2009 10:31 PM |
| Using this in initialization list?? | huili80@gmail.com | Newsgroup comp.language.c++ | 5 | 05-07-2009 11:11 AM |
| class variables and class methods | krishnapostings@gmail.com | Newsgroup comp.lang.python | 1 | 04-18-2009 03:15 PM |