News for hackers. Stuff that matters.

Ask HN: What should be added to the syntax of Python and why?

I recently came across the pipe library [0] which allows function chaining in a much more straightforward way:

    sum(fib() | where(lambda x: x % 2 == 0) | take_while(lambda x: x < 4000000))
I know in Lisp languages you can extend the syntax easily, but I was wondering if there are any needs for that in Python.

[0] https://pypi.org/project/pipe/

( 11 comments | 3 points | on HN )
Re:Ask HN: What should be added to the syntax of Python and ... by pid-1 on Monday July 31, 2023 @06:27PM (#36949726)
I don't know the name of this idiom, but the following is valid Python:

import pandas as pd

my_df = (

    pd.Dataframe(<fetch_data>)

    .join(<...>)

    .query(<...>)

    .sort_values(<...>)
)

I also use plotly like so:

my_plot = (

    px.line(<...>)

    .update_xaxes(<...>)

    .update_layout(<...)
)

Not exactly function chaining, but I guess it has a similar effect.

I'm happy with Python's syntax nowadays and much prefer seeing improvements on library / environment related things.

Re:Ask HN: What should be added to the syntax of Python and ... by rowanG077 on Monday July 31, 2023 @06:45PM (#36949892)
It's called a fluent interface.
Re:Ask HN: What should be added to the syntax of Python and ... by pm2222 on Monday July 31, 2023 @06:39PM (#36949826)
I want braces.
Re:Ask HN: What should be added to the syntax of Python and ... by backslash_16 on Monday July 31, 2023 @08:18PM (#36950707)
I would love separate variable initialization vs assignment. I know that ship has sailed.

It's interesting, I never cared about that until I did "Crafting Interpreters" and realized how separating those two actions helps clarify scopes.

Being realistic I know it's too large of a breaking change and will never land in the language but I can dream right?

Re:Ask HN: What should be added to the syntax of Python and ... by kazinator on Monday July 31, 2023 @10:09PM (#36951502)

  s/separating/not stupidly conflating/
In computer science, binding a variable and assignment are not only separated, but by an ideological chasm.
Re:Ask HN: What should be added to the syntax of Python and ... by vhcr on Monday July 31, 2023 @11:13PM (#36951881)
I don't see it being such a bad thing, it ensures variables always have a defined value.
Re:Ask HN: What should be added to the syntax of Python and ... by thek3nger on Tuesday August 01, 2023 @01:48AM (#36952778)
None propagation. It such a pain to constantly check for None when accessing very deep nested structures (as I had to do often with JSON data).

There is a PEP for this[1] but it is currently deferred.

[1]: https://peps.python.org/pep-0505/

Re:Ask HN: What should be added to the syntax of Python and ... by mdwalters on Tuesday August 01, 2023 @02:07AM (#36952865)
Multi-line lambda. It would be more easier to use than cramming a piece of code into a single line.
Re:Ask HN: What should be added to the syntax of Python and ... by eternityforest on Tuesday August 01, 2023 @04:21AM (#36953666)
Ditch the general purpose variable annotations thing and make strong typing types first class
Re:Ask HN: What should be added to the syntax of Python and ... by VoodooJuJu on Tuesday August 01, 2023 @10:31AM (#36956404)
Curly braces, to make whitespace insignificant. I don't like significant whitespace.
Re:Ask HN: What should be added to the syntax of Python and ... by gabrielsroka on Tuesday August 01, 2023 @10:55AM (#36956762)