5 ms·
> I regularly wish python required some sort type indication for function parameters You can use type annotations, either via the builtin lib or 3rd party libs
by closed 8y ago
> I regularly wish python required some sort type indication for function parameters
You can use type annotations, either via the builtin lib or 3rd party libs!
https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html
> do_x() if a.y()
I feel this. You can use `a.y() and do_x()`, but I'm not sure how people would react :o
- fhood 8y ago> You can use type annotations, either via the builtin lib or 3rd party libs! > https://docs.python.org/3/library/typing.html https://docs.python.org/3/library/typing.html I know, but tragically, because I am not forced to, I never do. I do appreciate that feature though. Maybe this will be the impetus I need to start taking advantage of it.
- computerfriend 8y agoI started doing this about a year ago and it has made me a better Python developer. Getting into the habit of always writing a type annotation was hard at first, but you quickly get used to it. It helps if you configure mypy to complain at you if the annotations are missing.
- dharmab 8y agoIf you're starting a new project you can force yourself by running mypy's strict mode in CI.
- lmm 8y agoYou'll have a much better experience in an ecosystem where typing is the widespread expectation, IME. Try an ML-family language if you haven't already - I find they combine the best parts of something like Python and something like Java. There was a post pushing F# earlier today.
- bakery2k 8y ago> `a.y() and do_x()` If you're happy to write it that way round, why not just use `if a.y(): do_x()`?
- fhood 8y agoAgainst pep-8 standards. I do end up using that sytax quite a bit anyway though.
- viraptor 8y ago> PEP8: This document gives coding conventions for the Python code comprising the standard library in the main Python distribution. It's fine not to follow PEP8 for your own software. It's a useful guide, not a law.
- closed 8y agoIt's worth noting the second can't be used inline. I don't know the history, but it seems like that might be one of the reasons `x() if y()` isn't allowed in python. [x() if y()] if it existed, might carry an explicit else None. On the other hand [x() and y()] does something different when y() is falsey.
- vram22 8y ago>I don't know the history, but it seems like that might be one of the reasons `x() if y()` isn't allowed in python. Maybe I misunderstood your context, but if not, conditional expressions do exist in Python: $ python Python 2.7.12 |Anaconda 4.2.0 (32-bit)| (default, Jun 29 2016, 11:42:13) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org >>> 1 if 1 == 1 else 2 1 >>> 2 if 1 == 1 else 2 2 >>> ^Z $ py -3 Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 1 if 1 == 1 else 2 1 >>> 2 if 1 == 1 else 2 2 >>> Using nested conditional expressions to classify characters: https://jugad2.blogspot.com/2017/04/using-nested-conditional-expressions-to.html https://jugad2.blogspot.com/2017/04/using-nested-conditional...
- vram22 8y ago
- earenndil 8y agoType annotations don't seem to actually have any effect, though? Consider: >>> def add(a: int, b: int): ... return a+b ... >>> add("a", "b") 'ab' This should be an error, even if it's a runtime error.
- camel_Snake 8y agoit's optional typing. Running it through mypy will result in an error. If you are using type annotations I don't see why you wouldn't also be using a proper IDE or testing your code.
- bakery2k 8y agoThere doesn't seem to be any interest in checking type annotations at run-time. Unfortunately, without doing so, type annotations look authoritative but are essentially just comments. I believe Julia is an example of a dynamically-typed language which does perform such checks at run-time - including as part of its support for multiple dispatch.
- nitely 8y agoMake mypy type checking part of the CI pipeline. Done.
- qquark 8y agoI've been using enforce for this purpose (https://github.com/RussBaz/enforce https://github.com/RussBaz/enforce). It provides decorators that enable type checking where wanted. This gives the advantage of migrating existing code to type checking at your own pace at least. On a general note I've come across a few cases, when pushing python's type annotations to their limits, that force you to put the type names in string quotes, and that makes the whole thing feel like a hack. It's better than nothing for my use cases, but if I remember correctly both mypy and enforce have problems in common that are probably coming from the way python itself is built, such as self reference in a class definition.
- quodlibetor 8y ago