What does colon equal (:=) in Python mean?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Peaceful Mind
--
Chapters
00:00 Question
00:26 Accepted answer (Score 260)
02:32 Answer 2 (Score 113)
02:54 Answer 3 (Score 62)
04:19 Answer 4 (Score 52)
05:18 Thank you
--
Full question
https://stackoverflow.com/questions/2600...
Accepted answer links:
[starting in Python 3.8]: https://docs.python.org/3/whatsnew/3.8.h...
[PEP 572]: https://www.python.org/dev/peps/pep-0572/
[pseudocode]: http://en.wikipedia.org/wiki/Pseudocode
Answer 2 links:
[PEP572]: https://www.python.org/dev/peps/pep-0572/
Answer 3 links:
[Walrus Operator]: https://stackoverflow.com/tags/walrus-op...
Answer 4 links:
[PEP 572]: https://www.python.org/dev/peps/pep-0572/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #colonequals
#avk47
ACCEPTED ANSWER
Score 385
Updated answer
In the context of the question, we are dealing with pseudocode, but starting in Python 3.8, := is actually a valid operator that allows for assignment of variables within expressions:
# Handle a matched regex
if (match := pattern.search(data)) is not None:
# Do something with match
# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
process(chunk)
# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]
# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]
See PEP 572 for more details.
Original Answer
What you have found is pseudocode
Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm.
:= is actually the assignment operator. In Python this is simply =.
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- There are certain styles, and your mileage may vary:
Pascal-style
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
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.
ANSWER 2
Score 130
PEP572 proposed support for the := operator in Python to allow variable assignments within expressions.
This syntax is available in Python 3.8.
ANSWER 3
Score 60
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.strip() for l in lines) if len(l) > 0]
ANSWER 4
Score 23
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)")