4 ms·
+1 for Reflex. Truly amazing.
by vishnudeva 2y ago
+1 for Reflex. Truly amazing.
- greener_grass 2y agoHow is it writing React without multi-line lambdas? They are everywhere in JavaScript and I couldn't imagine my day-to-day without them!
- Retr0id 2y agoRio components are classes, so you could create a named class method and reference it. Obviously not the same thing as multi-line lambdas but it'd be the pythonic approach imho.
- vishnudeva 2y agoI think my answer: I have no idea what multi-line lambdas are, probably explains why I find Reflex (or Rio/Streamlit, etc) amazing, haha For a person with zero front-end knowledge, it's a game changer.
- greener_grass 2y agoIn 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 less intuitive way for no obvious benefit.
- vishnudeva 2y agoAh I see! Thanks for elaborating! :)
- skeledrew 2y agoActually 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.
- greener_grass 2y agoThat's pretty janky - I don't think it would pass review in many places!
- skeledrew 2y agoInteresting. 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.
- greener_grass 2y agoBut 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?
- skeledrew 2y agoI 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.
- mixmastamyk 2y agoEither name them, or squeeze multiple expressions into a tuple. More can be done, now with walrus.
- wiseowise 2y ago> They are everywhere in any proper programming language FTFY.