4 ms·
That's pretty cool, shame it doesn't work in 3 too. Not as nice, but: (lambda x, y: x+y)(*(2, 3)) Seems to work
by anfractuosity 4y ago
That's pretty cool, shame it doesn't work in 3 too.
Not as nice, but:
(lambda x, y: x+y)(*(2, 3))
Seems to work
- klyrs 4y agoNow do lambda i,(x,y): x[i]+y[j] I never really understood the rationale here. Pep8 hates lambdas, which is also baffling. It seems as though tptb don't like lambdas, but there wasn't enough political will to remove them entirely, so they kneecapped them instead. Because... more lines = more readable?? But we've got a walrus operator now, because... less lines = more readable?
- sitkack 4y agoYou golf a lambda and then deride walrus in the same comment? Please explain yourself. And if you really want to make the perfect lambda, you can do that with code generation and frame walking/patching. http://farmdev.com/src/secrets/framehack/index.html http://farmdev.com/src/secrets/framehack/index.html
- wnoise 4y agoThe example lambda isn't "golfing". It's just plain more readable than naming the function. Introducing side-effects in the middle of expressions with the walrus operator is weird semantics, though admittedly sometimes very convenient.
- xigoi 4y agoI like the walrus operator for working with the regex module. if match := re.search(r"Score = (\d+)", data): print(match[1]) else: print("Not found")
- klyrs 4y agoThat wasn't golf. Are you going to tell me that this is more legible? lambda i, t: t[0][i] + t[1][i] edit: and... that frame hack thing is fun, but what does it have to do with anything? Also, note that I don't "deride" the walrus operator. But it does smack of inconsistency. Are we aiming for clean notation or not?
- sitkack 4y agoYou want tuple params back into an inlined function declaration? You can do that with a framehack. One of the examples from the link is Ruby style string interpolation, f-strings in Python, before f-string support. You can fight Python on lambda support, or you can build the language you want. It gives you the tools. I am not arguing about lambda legibility, but tilting at windmills rather than just walking around them. They can't move.
- abecedarius 4y agoDo you really use framehacks in production for the sake of syntactic sugar like this? It's fair that Python can do this sort of thing[0], but it seems like begging for trouble to make it load-bearing without a very strong reason. [0] That level of access was essential when I was working on https://codewords.recurse.com/issues/seven/dragon-taming-with-tailbiter-a-bytecode-compiler https://codewords.recurse.com/issues/seven/dragon-taming-wit...
- sitkack 4y agoIf the local sugar leads to a lower incidence of diabetes, then yes. I have never had a framehack spontaneously break. Self proclaimed Pythonistas of course make a sour face, like their God was offended, but that isn't a logical argument against. I really enjoy your compiler art, please keep it up. Are you doing anything with Wasm these days?
- abecedarius 4y agoThank you! I dislike style nazis too, e.g. carping when Peter Norvig's code won't pass PEP 8. I'm just leery of the expected cost in this kind of case. It can go on working for years until some new complication or some change in the ecosystem makes it suddenly create a really weird problem. Or when you want to try moving to a fancy new Python implementation, you find you have this friction. Matter of judgement where some chance of such messes is paid for by what it can do for you. (Of course when it's less "load bearing" the balance shifts.) With https://coverage.readthedocs.io/en/stable/ https://coverage.readthedocs.io/en/stable/ for example, it used bytecode hacks to do something you couldn't do otherwise, and that's unlikely to mess you up. I have had old C programs go crazy years later in a really hard to debug way because newer compilers may interpret your code like your ex-wife's divorce lawyer (as Kragen put it). Back in the day a lot of us thought we had a different kind of relationship with C compilers, and it'd be fine to code to that informal social contract. (Just a loose analogy.) I'm piddling away at https://github.com/darius/cant https://github.com/darius/cant these days. (Some of the motivation was feeling too confined by Python, actually.) No Wasm, but I'm happy it exists! I tried to make a system like it 20 years ago (Idel) and gave up too soon.
- tln 4y agoFYI here's the thread where tuple parameters were removed. https://mail.python.org/archives/list/python-dev@python.org/thread/LDSQZYLO6E5YG3HSN3MY4BX5CXF73K4Y/#WXPLAKXU7VO33KJSBH5UBR5TFJQPXY6C https://mail.python.org/archives/list/python-dev@python.org/...
- klyrs 4y ago> So the short answer is: yes, we are attached to it, we will miss it. No guarantees about throwing fits, though <wink>. Observe: me, throwing a fit.
- carapace 4y agoI cursed and swore for twenty full minutes when they removed tuple unpacking from parameters. God that pissed me off. Sure, I was one of only a handful of people who ever used it, and it had it's warts, but it was so elegant.
- klyrs 4y ago>>> Hey, we don't like this feature anymore, is anybody using it?" >> Yes, we're using it" > Okay, it's gone" [rage] The one thing I agree with in the thread is the confusion of foo = lambda (x,y): x+y foo(1,2) #boom I think a better approach would be to raise warnings when sole parameters are parenthesized, unless written as foo = lambda (x,y),: x+y which is kinda gross, but cogent with the weirdness of singletons: a, = [1] b = (1,) c = 1, Alas, I've never had Guido's ear, so I shout into the void.
- raymondh 4y agoPeople who respect the law and love sausage should never watch either of them being made ;-) The referenced thread isn't Python's finest hour. A: "Hey, this feature is slightly inconvenient to implement in AST, let's kill it." B: "Many users haven't heard of this, so let's kill it." C: "I find it useful and there's not real advantage in killing it". A&B: "Okay, it's gone". Really, you're better-off just enjoying a language that is mostly nice and mostly well-designed. Don't read the development threads. It will spoil the illusion.