I see Python examples with :=
and ::=
. I've got a good understanding of the walrus operator from an article on RealPython(The Walrus Operator: Python 3.8 Assignment Expressions). I also went through the BNF document in Wikipedia (Backus–Naur form). They seem identical to me. Could anyone please help me to understand the difference between BNF(::=) and the walrus(:=) in Python? It would be great if you can tell me which one I should use in what case with an example?
Asked
Active
Viewed 300 times
-4

sol1000
- 141
- 1
- 6
-
4BNF isn’t Python, it is a separate languageused to define the Python syntax - a metasyntax as your linked article already describes it. – MisterMiyagi Jan 29 '23 at 13:40
-
2`bnf` is metasyntax, `:=` is syntax. That's it. – RomanPerekhrest Jan 29 '23 at 13:41
-
1This is explained in the documentation here: https://docs.python.org/3/reference/introduction.html#notation – kaya3 Jan 29 '23 at 14:04
-
Note that there is an element of nondeterminism in BNF's `::=`. If you have ` ::= B|C|D` then in a derivation `` can be replaced by `B` or `C` or `D`. With `:=` there is no such nondeterminism. If `x := y` then the value of `x` in any expression in the scope of this assignment is `y`, end of story. – John Coleman Jan 29 '23 at 14:05
-
I see that you are trying to brush up this question but I'm not sure how – ultimately the premise is a wrong one and the sources practically spell it out ("used to describe the syntax of languages used in computing" in the Wikipedia article). We *cannot* tell you which one you should use because they aren't even the same language (it's akin to asking whether one should use Python assignment or Rust assignment operators). If you've actually seen "**Python examples** with `:=` and `::=`", citing those might be helpful. – MisterMiyagi Jan 30 '23 at 10:51
-
Thanks for answering my question. I found `::=` expression at https://docs.python.org/3/reference/lexical_analysis.html and `:=` at https://docs.python.org/3/whatsnew/3.8.html#assignment-expressions. I think the example that uses `::=` is not a python code. I think `::=` is used to explain the python syntax in the document. Am I correct? – sol1000 Feb 01 '23 at 15:30
-
@sol1000 Yes. Notice how the first section you link to is [*lexical* analysis](https://en.wikipedia.org/wiki/Lexical_analysis), i.e. how the language is structured/read. This is not defined in the language itself as a recursive definition wouldn't be useful there. – MisterMiyagi Feb 01 '23 at 15:33
-
@MisterMiyagi, thank you so much for your help. I can see the difference clearly now. – sol1000 Feb 02 '23 at 11:28
1 Answers
0
:=
is a part of Python syntax, but ::=
is not. It was my misunderstanding that I thought the code with ::=
was a Python code. It was an explanation about how to use the Python in the document. You won’t see the actual Python code with ::=
in it.

sol1000
- 141
- 1
- 6