Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the := operator?

Tags:

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

x := y 

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

like image 359
Cole Tobin Avatar asked May 01 '12 23:05

Cole Tobin


People also ask

What does := do in programming?

:= 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.

What is the := operator in Python?

There is new syntax := that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.

What does := mean in C?

:= is not a valid operator in C. It does however have use in other languages, for example ALGOL 68. Basically, for what you want to know, the := in this example is used to assign the variable PTW32_TRUE to localPty->wNodeptr->spin.

What does += in C++ mean?

+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A.


2 Answers

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.

like image 86
Mark Byers Avatar answered Sep 21 '22 17:09

Mark Byers


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

like image 29
Alex W Avatar answered Sep 20 '22 17:09

Alex W