I was reading the python documentation and noticed the word clause
being used in parts of sentences like: the try clause (the statement(s) between the try and except keywords) is executed
, or Loop statements may have an else clause
. What does clause
mean?
-
It's in your quote *the statement(s) between the try and except keywords* – Guy Aug 08 '22 at 04:49
-
@Guy what would it mean for `loop statements to have an else clause` (my second example)? don't else statements come by them self, just the word `else`? – Aug 08 '22 at 06:47
-
Have a look at https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement. And `else` will never be stand alone, it will come after a loop or, more commonly, `if`/ `elif`. – Guy Aug 08 '22 at 06:48
-
so clause can basically be somewhere, for example: `if something:`, something being the clause. While clause can also be something after that if statement like say a print() if that if statement turns out to be true, correct? – Aug 08 '22 at 06:52
-
No, `something` will be the condition. The clause will be everything inside the `if` block, i.e. from the next line until the code after the `if` is ended (marked by an indentation to the left). – Guy Aug 08 '22 at 06:55
-
would code inside a function `def myFunction():` be considered clause? – Aug 08 '22 at 07:08
1 Answers
Clause, or more precisely a definite clause grammar, is a part of the the grammar used to define a programming language. Here is how it's defined for compound statements:
A compound statement consists of one or more ‘clauses.’ A clause consists of a header and a ‘suite.’ The clause headers of a particular compound statement are all at the same indentation level. Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header’s colon, or it can be one or more indented statements on subsequent lines. compound statements
compound_stmt ::= ...
| while_stmt
...\
while_stmt ::= "while" assignment_expression ":" suite
...\
So the while_stmt
or the equivalent right side "while" assignment_expression ":" suite
is an example of a clause.

- 23,068
- 5
- 28
- 38
-
Isnt that what `syntax` is, basically the rules AKA grammar for the programming language? – Aug 08 '22 at 06:48
-
-