いくつかのコードサンプルで使用されているのを見:=
たことがありますが、説明が付いていることはありません。固有名を知らずにその使用をグーグルで検索することは正確には不可能です。
それは何をするためのものか?
いくつかのコードサンプルで使用されているのを見:=
たことがありますが、説明が付いていることはありません。固有名を知らずにその使用をグーグルで検索することは正確には不可能です。
それは何をするためのものか?
http://en.wikipedia.org/wiki/Equals_sign#In_computer_programming
コンピュータープログラミング言語では、等号は通常、値の同等性をテストするブール演算子(PascalやEiffelなど)を示します。これは、数学での記号の使用法と一致します。または、代入演算子(Cのような言語など)を示します。 )。前者の選択を行う言語は、多くの場合、代入演算子を示すためにコロン等号(:=)または≔を使用します。後者の選択を行う言語は、ブール等号演算子を示すために二重等号(==)を使用することがよくあります。
注:これは、検索して見つけましたcolon equals operator
In the statically typed language Go :=
is initialization and assignment in one step. It is done to allow for interpreted-like creation of variables in a compiled language.
// Creates and assigns
answer := 42
// Creates and assigns
var answer = 42
Another interpretation from outside the world of programming languages comes from Wolfram Mathworld, et al:
If A and B are equal by definition (i.e., A is defined as B), then this is written symbolically as A=B, A:=B, or sometimes A≜B.
■ http://mathworld.wolfram.com/Defined.html
■ https://math.stackexchange.com/questions/182101/appropriate-notation-equiv-versus
一部の言語は:=
、代入演算子として機能するために使用します。
多くのCSブックでは、等式演算子と区別するために、代入演算子として使用されてい=
ます。ただし、多くの高級言語では、割り当てはで=
あり、平等は==
です。
これは、代入演算子の古い(パスカル)構文です。それは次のように使用されます:
a := 45;
他の言語でも、おそらく同様の用途である可能性があります。
A number of programming languages, most notably Pascal and Ada, use a colon immediately followed by an equals sign (
:=
) as the assignment operator, to distinguish it from a single equals which is an equality test (C instead used a single equals as assignment, and a double equals as the equality test).
Reference: Colon (punctuation).
In Python:
Named Expressions (NAME := expr
) was introduced in Python 3.8. It allows for the assignment of variables within an expression that is currently being evaluated. The colon equals operator :=
is sometimes called the walrus operator because, well, it looks like a walrus emoticon.
For example:
if any((comment := line).startswith('#') for line in lines):
print(f"First comment: {comment}")
else:
print("There are no comments")
This would be invalid if you swapped the :=
for =
. Note the additional parentheses surrounding the named expression. Another example:
# Compute partial sums in a list comprehension
total = 0
values = [1, 2, 3, 4, 5]
partial_sums = [total := total + v for v in values]
# [1, 3, 6, 10, 15]
print(f"Total: {total}") # Total: 15
Note that the variable total
is not local to the comprehension (so too is comment
from the first example). The NAME
in a named expression cannot be a local variable within an expression, so, for example, [i := 0 for i, j in stuff]
would be invalid, because i
is local to the list comprehension.
I've taken examples from the PEP 572 document - it's a good read! I for one am looking forward to using Named Expressions, once my company upgrades from Python 3.6. Hope this was helpful!
Sources: Towards Data Science Article and PEP 572.
It's like an arrow without using a less-than symbol <= so like everybody already said "assignment" operator. Bringing clarity to what is being set to where as opposed to the logical operator of equivalence.
In Mathematics it is like equals but A := B means A is defined as B, a triple bar equals can be used to say it's similar and equal by definition but not always the same thing.
Anyway I point to these other references that were probably in the minds of those that invented it, but it's really just that plane equals and less that equals were taken (or potentially easily confused with =<) and something new to define assignment was needed and that made the most sense.
Historical References: I first saw this in SmallTalk the original Object Language, of which SJ of Apple only copied the Windows part of and BG of Microsoft watered down from them further (single threaded). Eventually SJ in NeXT took the second more important lesson from Xerox PARC in, which became Objective C.
Well anyway they just took colon-equals assiment operator from ALGOL 1958 which was later popularized by Pascal
https://en.wikipedia.org/wiki/PARC_(company)
https://en.wikipedia.org/wiki/Assignment_(computer_science)
Assignments typically allow a variable to hold different values at different times during its life-span and scope. However, some languages (primarily strictly functional) do not allow that kind of "destructive" reassignment, as it might imply changes of non-local state. The purpose is to enforce referential transparency, i.e. functions that do not depend on the state of some variable(s), but produce the same results for a given set of parametric inputs at any point in time.
For VB.net,
a constructor (for this case, Me = this in Java):
Public ABC(int A, int B, int C){
Me.A = A;
Me.B = B;
Me.C = C;
}
when you create that object:
new ABC(C:=1, A:=2, B:=3)
Then, regardless of the order of the parameters, that ABC object has A=2, B=3, C=1
So, ya, very good practice for others to read your code effectively
Colon-equals was used in Algol and its descendants such as Pascal and Ada because it is as close as ASCII gets to a left-arrow symbol.
The strange convention of using equals for assignment and double-equals for comparison was started with the C language.
In Prolog, there is no distinction between assignment and the equality test.