* 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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to