Variables and the keys in key-value pairs are both identifiers for values, so, on a conceptual level, these seem very similar ideas.
How would you explain the difference between these two concepts?
- Is it that key-value pairs are used in collections?
- Is it that variables point to a memory location?
- Or something else?
(If I look at code, I’m instinctively aware of the difference, the question’s more about how to explain it which doesn’t seem too obvious given that they do similar things.)
7
There is no real difference.
We can think of a variable as a name that maps to some value. All visible variables together make up the environment. This observation is important in some theoretical discussions, and is also used in some language implementations (mostly dynamic languages like Ruby). Especially objects can be represented in different ways – some may be choose static structs where an object member is a field in that struct (e.g. C++), others may use dynamic dictionaries which contain all members as key-value pairs (e.g. JavaScript).
The nice thing is that these are mostly equivalent to each other: you can see one as a generalization, the other as a performance optimization.
In sane languages, the environment is known at compile time, whereas the keys in some dictionary are generally only known at run time. This is the only useful difference: when the key of some mapping is known.
I wouldn’t say that’s exactly the case. I personally think of key-value pairs as tuples (a, b) : A * B
in the mathematical sense as an element of a cartesian product A x B
. Variables don’t even come into the picture. The key is just the first component of the tuple. Just because a key can be used as a convenient handle to the entire tuple is incidental and you’re confounding these two cases. You could just as easily flip the whole thing around and use the value to refer to the entire tuple.
5
The difference depends on the context of the language. While there is a difference for compiled languages (variable names have meaning at compile-time, whereas keys have meaning at run-time), in Python there is no difference, as variable names are actually stored in a dict:
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> globals()['foo'] = 'bar'
>>> foo
'bar'
>>> type(globals())
<class 'dict'>
Generally speaking, both are used like memory aliases. But, a variable more directly aliases a memory offset at compile time, and a key informs a memory mapping algorithm at runtime.
Conceptually, the primary difference is one of scope: Whether the alias operates at compile time versus runtime.
Functionally, the primary difference is one of runtime mutability (of all such aliases): Whether the collection of aliases can be changed at runtime. You can’t compile your code and add Int32 xyz
at runtime. But, you can add key xyz
to a collection. (With some interpreted languages being an exception.)
Practically, the primary difference is lookup strategy and efficiency: Variables hold memory offsets. Using them is fast. Keys are piped through a lookup algorithm that, comparatively speaking, eventually determines a memory offset and returns its contents.
Lots of work is done at runtime to make keys work. Variables are sort of just there.
This all assumes you’re referring to keys in the context of a collection. You can certainly have key-value pairs that have nothing to do with collections or finding things in memory. But, the keys in that context are far enough removed functionally that … well … I’m just assuming the question isn’t referring to that.