4 ms·
Not just for beginners, the proposed syntax looks horrible in general. It's moreover already possible to put this into one line: libpaths = [] if libpaths is
by catdog 8y ago
Not just for beginners, the proposed syntax looks horrible in general. It's moreover already possible to put this into one line:
libpaths = [] if libpaths is None else libpaths.split(":")
- gaius 8y agoYes, this PEP looks like it is trying to turn Python into Ruby where the ? suffix is common for a function that returns a bool. Also weird syntactic magic that you can't really figure out, you have to be told about. I wonder if the original author of the PEP is a "rubyist". Of course in Python [] is False and in Ruby it's true...
- hnra 8y agoLooks exactly like adding the ?? [1] and ?. [2] operators from C#. [1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator https://docs.microsoft.com/en-us/dotnet/csharp/language-refe... [2]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- gaius 8y agooperators from C# Why not go the whole hog then, and do a Option[1] ;-) [1] https://en.wikipedia.org/wiki/Option_type#F# https://en.wikipedia.org/wiki/Option_type#F#
- mmirate 8y agoOption is bubkes if the language itself still has the billion-dollar mistake; that's why F# and Scala are far less practical than OCaml. Python also has it with "None" (though since it is dynamically typed, it's arguable that from the perspective of any given type, every value of every other type is a fresh instance of the billion-dollar mistake). And sadly, removing None from Python and replacing it by Option - let alone removing Exception and letting Result (aka Either) take its rightful place - would cause huge amount of Python code to need rewriting. And it wouldn't even be worth it because of dynamic typing.
- marcosdumay 8y agoWhat does you gain from optionals if you can't determine types at compile time?
- dragonwriter 8y ago> What does you gain from optionals if you can't determine types at compile time? As well as type checking (which is relevant because Python has AOT checkers even if they are optional), Options, compared to bills, give you nestability, which is a runtime advantage not dependent on static type checking.
- dragonwriter 8y ago> Yes, this PEP looks like it is trying to turn Python into Ruby where the ? suffix is common for a function that returns a bool. No, it's nothing like the Ruby convention. It's much more like th C# operators.
- agumonkey 8y agodo people use x = x or [] or is it considered non idiomatic in python ?
- tomchristie 8y agoIt’s okay-ish. You do need to be careful about what falsey values `x` might be tho, as it could introduce edge-case bugs. Using `x = [] if x is None else x` is more precise.
- Asooka 8y agoI would really just write `if x is None: x = []`