Live data from Hacker News

Rio: Web apps in pure Python

github.com

171–180 of 205 posts

Re: Rio: Web apps in pure Python

#171

Earlier quoted context omitted.

In JavaScript you can do this: const f = (x, y) => { const z = x + y; const w = z * 2; return z - w + x; }; In Python, you cannot do this: f = ( lambda x, y: z = x + y; w = z * 2; return z - w + x; ) Instead, you need to pull it out into a def: def f(x, y): z = x + y; w = z * 2; return z - w + x; Sometimes, this is no big deal. But other times, it's damn annoying; the language forces you to lay out your code in a les…

Actually you can . If you really want a multi-line lambda with your example... ```f = lambda x, y: [ z := x + y, w := z 2, z - w + x, ][-1]``` * That version does look strange, as it uses a list in order to get that last calculation. But I often use lambdas to check results in parametrized tests, and they naturally spread to multiple lines without the list hack since they're chains of comparisons.

Using a list combined with the walrus operator is a clever hack, but it's nice to not be limited to expressions. In JS you can define the equivalent of a multi-line lambda function with any number of statements (which is helpful when you're passing a function as a callback e.g. in a React hook).

Re: Rio: Web apps in pure Python

#172

Earlier quoted context omitted.

Actually you can . If you really want a multi-line lambda with your example... ```f = lambda x, y: [ z := x + y, w := z 2, z - w + x, ][-1]``` * That version does look strange, as it uses a list in order to get that last calculation. But I often use lambdas to check results in parametrized tests, and they naturally spread to multiple lines without the list hack since they're chains of comparisons.

That's pretty janky - I don't think it would pass review in many places!

Interesting. I see HN has mangled my code block onto 1 line and replaced a couple stars, but the fixup should be obvious...

Yes, it is, it should, and that's exactly the point. It'd look janky even without the `[-1]`, and this is what the core devs are trying to protect against. Lambdas are anonymous functions meant only to be used in places where expressions are valid, such as function parameters and return values. There's even a linter warning if you assign one to a variable. All to help reduce the creation of janky-looking code, and that's a huge benefit for most developers.

Re: Rio: Web apps in pure Python

#173

Earlier quoted context omitted.

Actually you can . If you really want a multi-line lambda with your example... ```f = lambda x, y: [ z := x + y, w := z 2, z - w + x, ][-1]``` * That version does look strange, as it uses a list in order to get that last calculation. But I often use lambdas to check results in parametrized tests, and they naturally spread to multiple lines without the list hack since they're chains of comparisons.

Using a list combined with the walrus operator is a clever hack, but it's nice to not be limited to expressions. In JS you can define the equivalent of a multi-line lambda function with any number of statements (which is helpful when you're passing a function as a callback e.g. in a React hook).

It may be nice in the moment, but there's usually regret a few weeks/months down the line when trying to read that code, or angst for the next developer. There isn't that much more effort to just create a normal def to hold that increase in complexity suggested by the need for multiple statements. That's why functions were invented in the first place.

Re: Rio: Web apps in pure Python

#174

Earlier quoted context omitted.

That's pretty janky - I don't think it would pass review in many places!

Interesting. I see HN has mangled my code block onto 1 line and replaced a couple stars, but the fixup should be obvious... Yes, it is, it should, and that's exactly the point. It'd look janky even without the `[-1]`, and this is what the core devs are trying to protect against. Lambdas are anonymous functions meant only to be used in places where expressions are valid, such as function parameters and return values.…

But what if I want to use lambdas for more things?

Imperative programming only gets you so far.

Maybe this is a sign that people are using Python for grander things than it was designed for?

Re: Rio: Web apps in pure Python

#175

Earlier quoted context omitted.

Using a list combined with the walrus operator is a clever hack, but it's nice to not be limited to expressions. In JS you can define the equivalent of a multi-line lambda function with any number of statements (which is helpful when you're passing a function as a callback e.g. in a React hook).

It may be nice in the moment, but there's usually regret a few weeks/months down the line when trying to read that code, or angst for the next developer. There isn't that much more effort to just create a normal def to hold that increase in complexity suggested by the need for multiple statements. That's why functions were invented in the first place.

If a callback function gets really unwieldy then you should probably extract it from the call site and define it elsewhere, but that should happen because you decided to, not because the language's limitations coerced you into doing it. The lambda restrictions in Python are probably due to the complexities of parsing indentation based languages, and the clean code argument is just a helpful rationalization. I've never woken up in angst over the fact that I wrote a callback function with two statements in it.

Re: Rio: Web apps in pure Python

#176

Earlier quoted context omitted.

Interesting. I see HN has mangled my code block onto 1 line and replaced a couple stars, but the fixup should be obvious... Yes, it is, it should, and that's exactly the point. It'd look janky even without the `[-1]`, and this is what the core devs are trying to protect against. Lambdas are anonymous functions meant only to be used in places where expressions are valid, such as function parameters and return values.…

But what if I want to use lambdas for more things? Imperative programming only gets you so far. Maybe this is a sign that people are using Python for grander things than it was designed for?

I feel like there is something missing here. What's stopping you from using a normal def? Aside from the definition itself not being usable inline, there is nothing lambda does that def doesn't. And if you really want a definition close to the calling site, just define it there and then put the name where you want to pass it.

At the end of the day though there's really nothing to prevent you from creating janky code. Heck I saw a wild hack a couple weeks ago that allows for the creation of arbitrary custom syntax with pure Python, so you could create a multi-line lambda if you really want to that badly. But the widely adopted conventions exist for a reason.

Re: Rio: Web apps in pure Python

#177

Earlier quoted context omitted.

It may be nice in the moment, but there's usually regret a few weeks/months down the line when trying to read that code, or angst for the next developer. There isn't that much more effort to just create a normal def to hold that increase in complexity suggested by the need for multiple statements. That's why functions were invented in the first place.

If a callback function gets really unwieldy then you should probably extract it from the call site and define it elsewhere, but that should happen because you decided to, not because the language's limitations coerced you into doing it. The lambda restrictions in Python are probably due to the complexities of parsing indentation based languages, and the clean code argument is just a helpful rationalization. I've neve…

There may be a few restrictions due to complexity, but lambdas isn't one. This is about decent and sane design choice[0] for readability.

[0] https://www.artima.com/weblogs/viewpost.jsp?thread=147358

Re: Rio: Web apps in pure Python

#178

Earlier quoted context omitted.

If a callback function gets really unwieldy then you should probably extract it from the call site and define it elsewhere, but that should happen because you decided to, not because the language's limitations coerced you into doing it. The lambda restrictions in Python are probably due to the complexities of parsing indentation based languages, and the clean code argument is just a helpful rationalization. I've neve…

There may be a few restrictions due to complexity, but lambdas isn't one. This is about decent and sane design choice[0] for readability. [0] https://www.artima.com/weblogs/viewpost.jsp?thread=147358

From the blog post you linked: "But the complexity of any proposed solution for this puzzle is immense, to me: it requires the parser (or more precisely, the lexer) to be able to switch back and forth between indent-sensitive and indent-insensitive modes, keeping a stack of previous modes and indentation level."

He's saying exactly what I said: that parsing (or more precisely lexing) makes the problem complex, because Python uses indentation semantically. He then rationalizes avoiding the implementation complexity with a gut feeling: "But none of that takes away my gut feeling".

Re: Rio: Web apps in pure Python

#179

Another rather unconventional way to do this, though it can be powerful, is to combine GraalPython, JavaFX and jpro.one: https://www.jpro.one The primary advantage is that the built in set of components is pretty powerful (e.g. a real table view), everything runs server side, the Python can be JIT compiled, and from the user's POV it still feels like a web app (e.g. zoom is respected). You can "shell out" to Javascri…

I should caution that the jpro.one home page has some pretty obvious screen reader accessibility issues. Assuming the website is rendered with the framework itself, I wouldn't be comfortable using that framework in a real application unless someone did a comprehensive accessibility audit first.

Re: Rio: Web apps in pure Python

#180
post #58

Are there any other frameworks that have similar ideas, perhaps using a functional language?

elixir's liveview is perhaps the best known and arguably inspired a lot of the ones in other languages

yes but doesn't that expect view code that looks a lot like HTML and is usually styled with Tailwind?
Post reply on HN