what does := means simply?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed May 16 23:54:51 EDT 2018
On Thu, 17 May 2018 05:33:38 +0400, Abdur-Rahmaan Janhangeer wrote:
> what does := proposes to do?
Simply, it proposes to add a new operator := for assignment (or binding)
as an expression, as an addition to the = assignment operator which
operates only as a statement. The syntax is:
name := expression
and the meaning is:
1. evaluate <expression>
2. assign that value to <name>
3. return that same value as the result
A simple example (not necessarily a GOOD example, but a SIMPLE one):
print(x := 100, x+1, x*2, x**3)
will print:
100 101 200 1000000
Today, we would write that as:
x = 100
print(x, x+1, x*2, x**3)
A better example might be:
if mo := re.search(pattern1, text):
print(mo.group(0))
elif mo := re.match(pattern2, text):
print(mo.group(3))
elif mo := re.search(pattern3, text):
print(mo.group(2))
which today would need to be written as:
mo = re.search(pattern, text)
if mo:
print(mo.group(0))
else:
mo = re.match(pattern2, text)
if mo:
print(mo.group(3))
else:
mo := re.search(pattern3, text)
if mo:
print(mo.group(2))
--
Steve
More information about the Python-list
mailing list