Proposed new syntax

Steve D'Aprano steve+python at pearwood.info
Thu Aug 17 21:13:07 EDT 2017


On Thu, 17 Aug 2017 10:47 pm, Gregory Ewing wrote:

> Steve D'Aprano wrote:
>> Do you consider:
>> 
>> for foo in bar:
>>     if baz(foo):
>>         f(foo)  # append to a list, or print, or yield, as needed
>> 
>> declarative?
> 
> No. But the reason for that is not because it has the word
> "for" in it. The reason is that it's made up of statements.
> Statements are procedural, expressions are declarative.

Not necessarily.

If we can't agree on definitions for procedural and declarative, I'm not sure
that this conversation is going to go anywhere. But the established definition
is that:

- in procedural code (although possibly "imperative" may be a better term) you
have to explicitly control the flow of program state yourself

- in declarative code you don't, you state the desired outcome and the
interpreter is free to choose its own execution flow to meet that outcome.

Whether your code is made of statements or expressions or both is not relevant.
You can have declarative statements, if the interpreter is free to perform them
in any order.


Within a single language, it is common to have both procedural and declarative
statements. E.g. in Python for statements are clearly procedural flow control.
But "import" can be read as a declarative statement:

"Load this module, I don't care how you do it or where you have to search or
what order you do the searching, so long as you load this module."

Arguably its not *really* declarative, because we can implicitly control the
search order by manipulating sys.modules and sys.path and a few other ways. But
that's splitting hairs -- by that standard, SQL SELECT isn't declarative
either, because databases often give you a way to fine-tune the query plan.


>> SQL's SELECT ... WHERE is one of the canonical examples of declarative
>> programming:
> 
> Despite the fact that "select" is a verb in English. :-)

"Select" isn't flow control.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list