Cole Tobin
Cole Tobin

Reputation: 9429

What is the := operator?

In some programming languages, I see (ex.):

x := y

What is this := operator generally called and what does it do?

Upvotes: 30

Views: 118319

Answers (7)

Akhil S Panicker
Akhil S Panicker

Reputation: 1

:= it means "set equal to" An assignment with syntax

v := expr sets the value of variable «v» to the value obtained from expression «expr».

Example: X := B Sets the Definition of X to the value of B

Upvotes: 0

Alex W
Alex W

Reputation: 38193

This is a new operator that is coming to Python 3.8 and actually had a role in BDFL Guido van Rossum's early retirement.

Formally, the operator allows what's called an "assignment expression". Informally, the operator is referred to as the "walrus operator".

It allows assignment while also evaluating an expression.

So this:

env_base = os.environ.get("PYTHONUSERBASE", None)
if env_base:
    return env_base

can be shortened to:

if env_base := os.environ.get("PYTHONUSERBASE", None):
    return env_base

https://www.python.org/dev/peps/pep-0572/#examples-from-the-python-standard-library

Upvotes: 22

user10762593
user10762593

Reputation:

The symbol is called "becomes" and was introduced with IAL (later called Algol 58) and Algol 60. It is the symbol for assigning a value to a variable. One reads x := y; as "x becomes y".

Using ":=" rather than "=" for assignment is mathematical fastidiousness; to such a viewpoint, "x = x + 1" is nonsensical. Other contemporary languages might have used a left arrow for assignment, but that was not common (as a single character) in many character sets.

Algol 68 further distinguished identification and assignment; INT the answer = 42; says that "the answer" is declared identically equal to 42 (i.e., is a constant value). In INT the answer := 42; "the answer" is declared as a variable and is initially assigned the value 42.

There are other assigning symbols, like +:=, pronounced plus-and-becomes; x +:= y adds y to the current value of x, storing the result in x.

(Spaces have no significance, so can be inserted "into" identifiers rather than having to mess with underscores)

Upvotes: 6

Mark Byers
Mark Byers

Reputation: 838256

In all languages that support an operator := it means assignment.

  • In languages that support an operator :=, the = operator usually means an equality comparison.
  • In languages where = means assignment, == is typically used for equality comparison.

does := mean =?

I can't recall any languages where := means the same as =.


In MySQL := and = are both used for assignment, however they are not interchangeable and selecting the correct one depends on the context. To make matters more confusing the = operator is also used for comparison. The interpretation of = as either assignment or comparison also depends on context.

Upvotes: 33

Jerry Coffin
Jerry Coffin

Reputation: 490148

PL/I has (had?) both = and :=. = is used for both assignment and comparison -- the compiler tries to figure out which you meant based on context. When/if it decides to do comparison where you really meant assignment, you can use := to force assignment.

For example, consider x=y=0; In C (for one example) this would mean "assign 0 to y, then the result of that (also 0) to x."

In PL/I, it means compare y to 0, and then assign the Boolean result of that comparison to x (i.e., equivalent to x = y == 0; in something like C). If you (being sane, unlike the designers of PL/I) intended that to mean "assign 0 to x and y", you'd use x = y := 0; (or x := y := 0;).

Upvotes: 1

thejartender
thejartender

Reputation: 9379

A lot of languages use common operators. Generally the = is reserved for variable assignment and should not be viewed in a mathematical context if it is alone. Equality in some languages like Java and Bash is tested though ==

Upvotes: 1

Joey Dong
Joey Dong

Reputation: 109

I usually see it more in pseudocode where it means an assignment. Thus x := y means 'set the value of x to the value of y' whereas x = y means 'does the value of x equal the value of y?'

Upvotes: 10

Related Questions