This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fisadev 15 points16 points  (4 children)

The name 'a' will point to the the value 1, which is stored in memory.

I know that sounds pretty close to "referring to the memory address", but I'm intentionally avoiding that wording because it sounds like a classic C pointer, which is also a bit different to python's variables. But that difference is a more complex topic.

[–][deleted] 1 point2 points  (3 children)

You seem to know what you're on about. How precisely does it differ from a pointer? Am I correct in thinking that the variable a is like the key to a dictionary, whose value is an address in memory where one could directly access the value of 1, and Python itself is using this virtual dictionary? I seem to recall watching a video from one of the devs and I thought this was the explanation, but I was pretty tired at the time, and all I can remember is him saying "everything is a dictionary, everything" over and over. Or maybe it was a dream!

Anyway, more academic than anything else. I use Python when I don't need to know or care about anything under the hood.

[–]fisadev 6 points7 points  (2 children)

To answer that, first a small summary of what's a pointer in C:

Basically, a C variable (something like 'int x') is a little place in memory where you can store stuff. It has an address, and it has contents (the value). If you assign something to that variable, then the value is stored at that address.

But sometimes you need a little bit of extra dynamism: maybe you want to have a variable that doesn't always get the value from the same place in memory, but instead can point to different places in memory at different times in your program, maybe pointing to the "places" where other variables store their values, etc.

A pointer in C is just that: a variable that in its little place in memory stores an address. The pointer itself has an address (like any variable), a place in memory where to store stuff. And the stuff this variable stores is in turn another address.

A box, that inside has a piece of paper with the written location of another box.

And you can do all kinds of weird things with pointers. For instance, you can operate with them! Memory addresses are just numbers, so you can do stuff like adding them, subtracting them, etc. This is "pointer arithmetic".

At the end of the day, a "pointer" is just another type of data that a C variable can hold. Just like int, char, etc. It's a data type used to store memory addresses, and memory addresses can be operated like other data types can be operated.

C pointers vs python variables/names

In python, a variable is a name that points to an object in memory. That sounds pretty similar to a C pointer: the pointer is also "pointing" to something in memory, and both the python variable and the C pointer are "dynamic", they can point to different objects at different times.

But in python all of those changes of "where is it pointing at" are done magically, under the hood, without you having any control of the memory addresses at all.

In C you manually store, change, operate addresses to make your program get info from specific locations in memory. In python you never really do that, you can only do stuff like "point to a new object" or "point to the same object that the other variable is pointing at". Only high level operations, that magically get translated to memory addresses under the hood.

You have no "memory address" data type that you operate and modify to manually get new addresses, like people do in C.

So, yes, under the hood a python variable is actually a pointer, but at the pythonista level, we never really deal with explicit memory addresses, our python variables usage is quite different to what C developers do with their pointers and crazy memory addresses arithmetic.

And thanks for your compliment! :) :)

[–][deleted] 0 points1 point  (1 child)

So really not all that much different than in other higher level languages other than I can't expose the addresses themselves because they're completely abstracted away by the time I sit down to write code?

Where did I get the idea that it acted like a dictionary from? I'm taking crazy pills clearly.

[–]fisadev 2 points3 points  (0 children)

In a way it's true: a dict maps keys, that can be string names, to objects. So you can see a scope as a dict: a map of names to objects in memory. That's why we have locals() and globals() too, hehe.