You can use type annotations, either via the builtin lib or 3rd party libs!
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
You can use type annotations, either via the builtin lib or 3rd party libs!
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
>>> 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.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.
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.
With pep-0563, python3.7, and `from __future__ import annotations` this should no longer be necessary. Those are some pretty detailed caveats, but I have been using it in places where I can and it is so nice.
For actual projects you can use a static type checker like mypy.
If you're happy to write it that way round, why not just use `if a.y(): do_x()`?
It's fine not to follow PEP8 for your own software. It's a useful guide, not a law.
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.
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...
>>> from __future__ import print_function
>>> for i in range(3, 5):
... print(i, "is", "odd" if i % 2 == 1 else "even")
...
3 is odd
4 is even
which can be shortened to this: >>> for i in range(3, 5):
... print(i, "is", "odd" if i % 2 else "even")
...
3 is odd
4 is even
Now print() is printing 3 values: i, "is", and either "odd" or "even" based on the boolean condition.That works in both Python 2 and 3.
> 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.
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.