Proposed new syntax

Steve D'Aprano steve+python at pearwood.info
Wed Aug 16 11:39:21 EDT 2017


On Thu, 17 Aug 2017 12:28 am, Ben Finney wrote:

> Steve D'Aprano <steve+python at pearwood.info> writes:
> 
>> If he wanted declarative semantics, why didn't he argue for
>> declarative syntax like "select...where", instead of choosing
>> procedural syntax which matches the actual procedural semantics given?
> 
> The syntax “f(foo) for foo in bar if baz(foo)” is nicely declarative.

Do you consider:

for foo in bar:
    if baz(foo):
        f(foo)  # append to a list, or print, or yield, as needed

declarative?

My understanding of "declarative" agrees with Wikipedia:

"In computer science, declarative programming is a programming paradigm ... that
expresses the logic of a computation without describing its control flow."

https://en.wikipedia.org/wiki/Declarative_programming

For-loops are a classic example of explicitly specifying control flow, so I'm
surprised that you seem to think they are declarative.


SQL's SELECT ... WHERE is one of the canonical examples of declarative
programming:

SELECT *
 FROM  Book
 WHERE price > 100.00

returns matching books (those with a price over 100) in arbitrary order (unless
you specify the "ORDER BY" clause). But more importantly than the order of
results, the order of data accesses is not specified by the SELECT statement.
Instead, the SQL compiler generates a "query plan" that specifies how to access
the data:

https://en.wikipedia.org/wiki/Query_plan

independently of the programmer. (Although many databases offer additional tools
for manually fine-tuning the query plan.)

If instead we wrote:

for row in Book:
    if row.price > 100:
        print(row)  # for example

I think that is specifying the control flow, and therefore not declarative. Do
you agree?



[...]
> I'm responding to the proposal by pointing out that, unlike today's
> Python, the proposed behaviour would violate those semantics.

In what way?

Do you feel the same way about itertools.takewhile?



-- 
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