views:

921

answers:

7

I'm trying to cross compile an in house language(ihl) to Python.

One of the ihl features is pointers and references that behave like you would expect from C or C++.

For instance you can do this:

a = [1,2];  // a has an array 
b = &a;     // b points to a
*b = 2;     // derefernce b to store 2 in a
print(a);   // outputs 2
print(*b);   // outputs 2

Is there a way to duplicate this functionality in Python.

I should point out that I think I've confused a few people. I don't want pointers in Python. I just wanted to get a sense from the Python experts out there, what Python I should generate to simulate the case I've shown above

My Python isn't the greatest but so far my exploration hasn't yielded anything promising:(

I should point out that we are looking to move from our ihl to a more common language so we aren't really tied to Python if someone can suggest another language that may be more suitable.

A: 

Negative, no pointers. You should not need them with the way the language is designed. However, I heard a nasty rumor that you could use the: ctypes module to use them. I haven't used it, but it smells messy to me.

windfinder
I know that there are no pointers in Python:)My goal is to cross compile my existing language into Python so I don't have to support a runtime any longer.What Python would I generate to simulate my example above?
chollida
+9  A: 

You may want to read Semantics of Python variable names from a C++ perspective. The bottom line: All variables are references.

More to the point, don't think in terms of variables, but in terms of objects which can be named.

Stephan202
Thank you for the link Stephan!
chollida
Of course it has variables; a variable in Python is a named object. Saying "Python doesn't have variables" is just confusing things unnecessarily.
Glenn Maynard
@Glenn: I take variable to mean a 'named memory location'. Admittedly, that may not be the correct definition. Though this sentence at Wikipedia, if I interpret it correctly, appears to agree with me: http://en.wikipedia.org/wiki/Variable_%28programming%29#In_source_code
Stephan202
Not if you are thinking of variables as a fixed in memory space, which is what it is in C.
Lennart Regebro
WP says "an identifier that is linked to a value", which I think describes Python just fine. (The specific sentence you linked to doesn't contradict this: it says a variable name is one way to get to a memory location, not that the memory location is the variable.) If you think Python "doesn't have variables", then most other programming languages I've ever used don't, either! :-)
Ken
Alright, I changed the wording. Hopefully it's now less controversial :)
Stephan202
+3  A: 

Everything in Python is pointers already, but it's called "references" in Python. This is the translation of your code to Python:

a = [1,2]  // a has an array 
b = a     // b points to a
a = 2      // store 2 in a.
print(a)   // outputs 2
print(b)  // outputs [1,2]

"Dereferencing" makes no sense, as it's all references. There isn't anything else, so nothing to dereference to.

Lennart Regebro
Thanks for the answer. What is concerning me is the last line of your answer. For the correct semantics in my old language I'd need be to now print out 2 as in the old language it's a pointer to a.
chollida
That's not a pointer then, but an alias, where you point one name to another name. You can't do that in Python, as that's completely unnecessary. In Python you would simply call it "a" the whole time. No aliases needed.
Lennart Regebro
+1  A: 

As others here have said, all Python variables are essentially pointers.

The key to understanding this from a C perspective is to use the unknown by many id() function. It tells you what address the variable points to.

>>> a = [1,2]
>>> id(a)
28354600

>>> b = a
>>> id(a)
28354600

>>> id(b)
28354600
Unknown
+18  A: 

This can be done explicitly.

class ref:
    def __init__(self, obj): self.obj = obj
    def get(self):    return self.obj
    def set(self, obj):      self.obj = obj

a = ref([1, 2])
b = a
print a.get()  # => [1, 2]
print b.get()  # => [1, 2]

b.set(2)
print a.get()  # => 2
print b.get()  # => 2
ephemient
+1 class `ref` now models a memory location.
Stephan202
You might not want to use the name "ref", since that's the same name as the weakref reference. Perhaps "ptr" or something. A sensible implementation, though.
Paul Fisher
I was thinking of SML, where this is called `'a ref`, but yeah, it would be better to choose a more unique name. Not sure that `ptr` makes all that much sense, though; it's not actually a pointer, it's more like a single container...
ephemient
Note that this is only even meaningful when using immutable objects like ints or strings. For mutable objects a=Something(); b=a; is perfectly enough.And even with immutable objects it's pretty pointless...
Lennart Regebro
You can alternately override `__call__` to implement getting and setting, which is more similar to how a weakref behaves.
Miles
+1  A: 

This is goofy, but a thought...

# Change operations like:
b = &a

# To:
b = "a"

# And change operations like:
*b = 2

# To:
locals()[b] = 2


>>> a = [1,2]
>>> b = "a"
>>> locals()[b] = 2
>>> print(a)
2
>>> print(locals()[b])
2

But there would be no pointer arithmetic or such, and no telling what other problems you might run into...

Anon
This probably isn't the greatest idea, because changes to locals() aren't guaranteed to be reflected in the environment.
Paul Fisher
Also, you can't pass `b` into other functions this way.
ephemient
As per Paul's comment - I tested it in a function, and it didn't even change the variable in there. Can change globals that way in a function, but locals() apparently is just giving a copy of the dict when in a function.
Anon
And still: Why? If you want to reference a, just use a. This is still just trying to make something that is easy in Python, but complicated in C, the complicated C way.
Lennart Regebro
@Lennart - I don't think anybody would argue its the way to write Python code. But it seems like what the OP was after was a simple mechanistic way to convert his code to working Python. And if that is the priority, then maybe finding a way to map pointers and dereferencing onto Python might achieve that goal, admittedly at the cost of resulting hideous - but working - Python code.
Anon
+4  A: 

If you're compiling a C-like language, say:

func()
{
    var a = 1;
    var *b = &a;
    *b = 2;
    assert(a == 2);
}

into Python, then all of the "everything in Python is a reference" stuff is a misnomer.

It's true that everything in Python is a reference, but the fact that many core types (ints, strings) are immutable effectively undoes this for many cases. There's no direct way to implement the above in Python.

Now, you can do it indirectly: for any immutable type, wrap it in a mutable type. Ephemient's solution works, but I often just do this:

a = [1]
b = a
b[0] = 2
assert a[0] == 2

(I've done this to work around Python's lack of "nonlocal" in 2.x a few times.)

This implies a lot more overhead: every immutable type (or every type, if you don't try to distinguish) suddenly creates a list (or another container object), so you're increasing the overhead for variables significantly. Individually, it's not a lot, but it'll add up when applied to a whole codebase.

You could reduce this by only wrapping immutable types, but then you'll need to keep track of which variables in the output are wrapped and which aren't, so you can access the value with "a" or "a[0]" appropriately. It'll probably get hairy.

As to whether this is a good idea or not--that depends on why you're doing it. If you just want something to run a VM, I'd tend to say no. If you want to be able to call to your existing language from Python, I'd suggest taking your existing VM and creating Python bindings for it, so you can access and call into it from Python.

Glenn Maynard
Yeah, my `ref` is basically the degenerate 1-element-only case of `list`. A 1-`tuple` would be lower overhead, but unfortunately those are not mutable.
ephemient
By the way, I think what you *really* want is a cell--that's what Python uses to store closures. Unfortunately, those are an implementation detail that aren't exposed--you can't instantiate them directly.
Glenn Maynard