From: Alf P. Steinbach on 7 Feb 2010 22:27
* Steve Holden:
> Alf P. Steinbach wrote:
>> * MRAB:
>>> Alf P. Steinbach wrote:
>>>> * 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
>>>> 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.
> 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.,
From: Steven D'Aprano on 7 Feb 2010 23:01
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.
From: T on 7 Feb 2010 23:15
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?
From: Steven D'Aprano on 8 Feb 2010 00:12
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)."
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.)
Unless otherwise stated, references are opaque and coders need not care
how the reference mechanism is implemented, see e.g.:
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.
From: Alf P. Steinbach on 8 Feb 2010 00:12
* 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
<function forward at 0x00DB7D20>
>>> id( turtle.forward )
>>> hex( id( turtle.forward ) )
> 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.
Cheers & hth.,