Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

What is a variable?

$
0
0

You can visualize a variable 1 as a box that contains a thing 2 . For example, this variable called x contains an integer 0:

This other variable called most_recent_email contains a pointer (more below) which we draw with an arrow.


What is a variable?

Beyond these basics, programming languages disagree what a variable is and can do. I will use variable to mean the green box, identifier to mean the name (like x or most_recent_email ), and value to refer to a thing that goes inside the variable.

Declaration

Declaration creates a new variable out of thin air 3 . We'll draw a bomb inside the box since declaration by itself does not put a value in the variable, so errors will occur if you try to use what is inside.


What is a variable?
Language Code C int x; Rust let x: i32; Java int x; Java final int i;
Allocation

Allocation is like declaration, but creates a variable without an identifier. It also creates a pointer pointing at the variable.


What is a variable?
Language Code 4 C malloc(1000000000); C++ new int[1000000000]; Initialization

Initialization , which is often done at the same time as or immediately after declaration or allocation, is putting a value into a variable for the first time. You can initialize variables you previously declared:


What is a variable?
Language Code Note C x = 7; Rust x = 7;

You can declare and initialize at the same time (we introduce the lock drawing for the optional const or final which we will come back to in a second):


What is a variable?
Language Code Java final int i = 7; C/C++ int const i = 7; php 6 $x = 7; R 6 x <- 7

In a very common pattern, you can allocate, initialize, declare, and initialize all in quick succession. The x variable contains a pointer. Many languages call this kind of variable a reference: 5


What is a variable?
Language Code python x = 7 ES6 let x = 7; Java Integer x2 = new Integer(7); Haskell let i = 7 in .... C++ int* x2 = new int; *x2 = 7; C++ int * const i2 = new int; *i2 = 7; Assignment

Assignment puts a new value into a variable, wiping out the previous value. It is like erasing a chalkboard and writing something new on it.


What is a variable?

Examples of assignment:

Language code C x = 9; Java x = 9; PHP $x = 9;

Also common is assignment of a variable that contains a pointer (many languages call this kind of variable a reference):


What is a variable?
Language Code Python x = 9 C++ x2 = new int; *x2 = 9;

Not all programming languages and not all variables support assignment. Variables that don't support assignment are referred to in many programming languages as const . Using the i examples from above, all of these will cause an error:

Language Code Java i = 9; C/C++ i = 9; C++ i2 = new int; *i2 = 9; Identifier Aliasing

Some languages let you attach more than one identifier to the same variable. This is known as aliasing although PHP calls it "references".


What is a variable?
Language Code PHP $y =& $x; Pointer Aliasing

Many languages let you create multiple pointers to a single allocated variable. This is also sometimes known as aliasing .


What is a variable?
Language Code Python y = x C++ int* y2 = x2; Footnotes

1 some variables in computers science are actually "free variables" which are different from what is described here. parameter assignment, closures, currying, unification, and variables in mathematics are all topics for another post. The word "variable" means too many things.

2 ultimately all variables store sequences of bits, but this is a topic for another post

3 actually, out of a big pool of memory

4 the compiler I tried optimized out these non-sensical examples, which you should never actually write into code

5 these examples don't actually store 7 -- for example in Python the value actually is made up of two separate values, an instance of PyObject_HEAD and a 7. In the Haskell example, the value is probably actually a Thunk that will evaluate to 7

6 PHP secretly does an allocation of a zval here, but tries to make it look like its variables directly store values. R does something similar.


Viewing all articles
Browse latest Browse all 9596

Trending Articles