3 ms·
OK, I'm a huge python homer, but I'll take a hack at that. Sometimes, for reasons of style and clarity, I want to define a function inline, but python doesn't
by llimllib 17y ago
OK, I'm a huge python homer, but I'll take a hack at that.
Sometimes, for reasons of style and clarity, I want to define a function inline, but python doesn't allow me to. It forces me to do:
def x(bar, fram):
z = foo(bar, fram)
baz(z)
button.onClick(x)
When in some cases, I would like to express it as:
button.onClick(
def (bar, fram):
z = foo(bar, fram)
baz(z)
)
So I get that it's never necessary, but I find it sometimes limiting as an author who cares strongly about how his code is organized.
- jmillikin 17y agoThe example given (I know it's synthetic, but still) can be written easily as a lambda: button.onClick(lambda bar, fram: baz(foo(bar, fram))) The problem with embedding procedure definitions within parameter lists is indentation. Languages such as JavaScript or Ruby use explicit syntax for indentation, which allows a degree of flexibility in how procedure literals are defined. Python uses only whitespace, so trying to extend the grammar to support arbitrary procedure literals would be very difficult (according to Guido).
- mbreese 17y agoI kinda like the way syntax the parent used. It seemed to make sense and work with the whitespace.
- llimllib 17y agofor the same reasons of style and clarity, I prefer my imaginary syntax to the lambda you've defined. And if you want to make it impossible, just imagine it's "print baz(z)". Anyway, I'm familiar with why Guido doesn't want them, and I totally respect it, and I write 99% of my personal code in python, so I clearly don't think it's a huge deal. It's just something I wish python did have, is all.
- viraptor 17y agoI see the problem when using third-party libraries, but if I was designing my own... Objects are pretty cheap in python: class Somebutton(Button): def onClick(self): ..... def onSomethingElse(self): ..... only one line of overhead (class line). And I'm pretty sure you can hack __dict__ in init, or use metaclasses so that the events are chained automatically, even without calling `Button.onClick(self)`. You can construct / add the object to it's container the same way (Somebutton(), instead of Button())
- 0x44 17y agoShould you have to hack __dict__ in init to get functionality that would be more easily/clearly expressed using an anonymous function?
- moe 17y agoNot trying to defend python's decision here but I have honestly yet to see an example where anonymous function really led to better code. In my experience (mostly JS and AS) they rather usually lead to little pieces of logic scattered all over the place and insufficient decoupling.
- j_baker 17y ago"So I get that it's never necessary, but I find it sometimes limiting as an author who cares strongly about how his code is organized." Nothing other than an if statement, a goto, and some basic arithmetic is needed in a programming language. It's all syntactic sugar. I agree that such a construct would be useful. However, it's more about if it's worth complicating the language over. I don't necessarily know how complex that would make things, but I can definitely see that a case can be made that it would be more trouble than it's worth considering the way indentation is handled.