what does := means simply?

Abdur-Rahmaan Janhangeer arj.python at gmail.com
Thu May 17 04:18:28 EDT 2018


thank you very much @steve

i guess it will make py fly more than ever !

Abdur-Rahmaan Janhangeer
https://github.com/Abdur-rahmaanJ

On Thu, 17 May 2018, 07:57 Steven D'Aprano, <
steve+comp.lang.python at pearwood.info> wrote:

> 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
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list