What is the := operator?

南楼画角 提交于 2021-01-20 23:54:12

问题


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

x := y

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


回答1:


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.




回答2:


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?'




回答3:


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




回答4:


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)




回答5:


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




回答6:


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;).



来源:https://stackoverflow.com/questions/10405820/what-is-the-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!