What does colon equal (:=) in Python mean?

你说的曾经没有我的故事 提交于 2019-12-09 07:46:32

问题


What does the := operand mean, more specifically for Python?

Can someone explain how to read this snippet of code?

node := root, cost = 0
frontier := priority queue containing node only
explored := empty set

回答1:


What you have found is pseudocode

http://en.wikipedia.org/wiki/Pseudocode

Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm.

the := operator is actually the assignment operator. In python this is simply the = operator.

To translate this pseudocode into Python you would need to know the data structures being referenced, and a bit more of the algorithm implementation.

Some notes about psuedocode

:= is the assignment operator or = in python

= is the equality operator or == in python

Note that there are certain styles of pseudocode and your mileage may vary:

Pascal Style PseudoCode

procedure fizzbuzz
For i := 1 to 100 do
    set print_number to true;
    If i is divisible by 3 then
        print "Fizz";
        set print_number to false;
    If i is divisible by 5 then
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
end

C-Style Pseudocode

void function fizzbuzz
For (i = 1; i <= 100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}

Note the differences in brace usage and assignment operator.




回答2:


PEP572 proposed support for the := operator in Python to allow variable assignments within expressions.

This syntax will be available in Python 3.8.




回答3:


The code in the question is pseudo-code; there, := represents assignment.

For future visitors, though, the following might be more relevant: the next version of Python (3.8) will gain a new operator, :=, allowing assignment expressions (details, motivating examples, and discussion can be found in PEP 572, which was provisionally accepted in late June 2018).

With this new operator, you can write things like these:

if (m := re.search(pat, s)):
    print m.span()
else if (m := re.search(pat2, s):
    …

while len(bytes := x.read()) > 0:
    … do something with `bytes`

[stripped for l in lines if len(stripped := l.strip()) > 0]

instead of these:

m = re.search(pat, s)
if m:
    print m.span()
else:
    m = re.search(pat2, s)
    if m:
        …

while True:
    bytes = x.read()
    if len(bytes) <= 0:
        return
    … do something with `bytes`

[l for l in (l.stripped() for l in lines) if len(l) > 0]



回答4:


Happy 3.8 Release on 14th of October

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.

In this example, the assignment expression helps avoid calling len() twice:

if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected <= 10)")

https://docs.python.org/3/whatsnew/3.8.html



来源:https://stackoverflow.com/questions/26000198/what-does-colon-equal-in-python-mean

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