4 ms·
>>> Are there other languages that do this so nicely? It's the perfect blend of OO and functional. Python, where everything is an object.
by _ZeD_ 3y ago
>>> Are there other languages that do this so nicely? It's the perfect blend of OO and functional.
Python, where everything is an object.
- c-hendricks 3y agoIn Python, can typings define a property added to a function? Example, I have some Redux helpers that are functions, but those functions also define a `.actionType` property. TypeScript handles that. edit: accidentally wrote "object" instead of "function"
- dataangel 3y agoI believe you could have a Protocol that requires the property and that the object is Callable.
- c-hendricks 3y agoAh, that's not really that different than in TypeScript then. There you have to define a callable interface, and can add whatever properties you want interface SomethingCallableWithAnExtraProperty { // This means you can call it like a function. (args: Whatever): SomethingReturned // And since it's an interface, you can still do interface-y things anotherProperty: string }
- nerdponx 3y agoThe typical approach to this in Python is to define a callable class instead. If you define the __call__() method on a class, instances of the class will be callable. class IntAdder: def __init__(self, x: int) -> None: self.x = x def __call__(self, y: int) -> int: return self.x + y def __str__(self) -> str: return f"({self.x} + ??)" add1 = IntAdder(1) assert add1(3) == 4 print(add1) # (1 + ??) As mentioned in the sibling comment, you can define a Protocol which is analogous to an interface, so if you had a protocol like this: from typing import Protocol, TypeVar T = TypeVar("T") class ValueUpdater(Protocol[T]): def __call__(self, y: T) -> T: ... Then IntAdder would be considered a subclass of ValueUpdater[int] by the type checker. Demo: https://mypy-play.net/?mypy=1.5.1&python=3.11&flags=strict&gist=345aeaaa8bfb0eef387b930bdf6ee295 https://mypy-play.net/?mypy=1.5.1&python=3.11&flags=strict&g...
- flylikeabanana 3y agoFP in python is painful without tail call elimination and the higher-order function syntax is so clunky
- nightpool 3y agoJS also doesn't have TCE, but for Python even just the lambda limitations are surprisingly annoying. I can't tell you how many times i've been frustrated because it's nearly impossible to put a print statement into a python lambda
- sixo 3y agoThat's a mismatch between Python's choice of lambda syntax and the use of whitespace instead of curly braces.
- fullsailor 3y agoJS technically does have TCE (specified in ES6[1]), but only the JavaScriptCore runtime used by Safari/WebKit implements it[2]. [1]: https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit/ https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-... [2]: https://kangax.github.io/compat-table/es6/#test-proper_tail_calls_(tail_call_optimisation) https://kangax.github.io/compat-table/es6/#test-proper_tail_...
- throwaway287391 3y agoAs a Python enjoyer, why do we want to shove so much into lambdas rather than just doing an inline function `def`? Is it the fact that you have to give it a name? If so I'd say just using some generic name like `{f,fn,func(tion),callback,etc}` is fine (at least as much as an anonymous function is), and to me would usually be more readable than an inline definition of a multi-statement lambda would be. Or maybe it's the fact that lambdas are allowed in the first place, so people are going to use them, and then when you want to debug a lambda you'll probably have to go to the trouble of changing it to a function? That is a fair complaint if so. In any case I can see how it could be annoying if you're more used to a language full of complex inline lambdas.