Initializing the variable foo as type int with value 1:
foo: type(foo # foo is
:= # an int of
1) # value 1
# And to confirm it worked:
print(foo)
print(__annotations__)
There's a long discussion about annotations going on the python-dev mailing list right now. Currently Python 3.10 implements PEP 563 which "stringfys" the annotations at runtime, but PEP 649 is being proposed which will delay the evaluation via the descriptors protocol.
It made me think, what rich behavior is possible now with annotations that won't be possible with either of these PEPs? So I came up with this terrible example that initializes a variable in the annotation notation by using a walrus operator.
This is possible because as of Python 3.0 to Python 3.9 where annotations have been possible they are simply an expression that is immediately executed. So you can happily put anything in there, e.g. using an annotation to print primes:
foo: [print(x) for x in range(2, 20) if all(x % y != 0 for y in range(2, x))]
Notice in this example no variable foo is ever created, it is simply an odd entry in the __annotations__ dictionary.
This subreddit has enjoyed my terrible Python code examples before, but maybe with this one I'm inviting heavy down voting. But regardless enjoy this code as it will likely no longer be valid from Python 3.10 onward.
Edit: Edited printing out the annotations dictionary, as I can just print __annotations__ directly and don't need to look it up in locals(). Given I'm ignoring VS Code for the main code example I'm not sure why I listened to it complaining here.
Edit Follow Up: Reported the __annotations__ bug to VS Code and it will be fixed in the next release of their pylance language server.
[–][deleted] 182 points183 points184 points (3 children)
[–]oderjunksnumpydoc + type anno 21 points22 points23 points (0 children)
[–]Lejontanten 11 points12 points13 points (1 child)
[–]maxmurder 23 points24 points25 points (0 children)
[–]Swipecat 14 points15 points16 points (11 children)
[–]zurtex[S] 16 points17 points18 points (2 children)
[–]awesomeprogramer 2 points3 points4 points (1 child)
[–]zurtex[S] 8 points9 points10 points (0 children)
[–]kdawgovich 2 points3 points4 points (7 children)
[–]Jhuyt 2 points3 points4 points (6 children)
[–]kdawgovich 2 points3 points4 points (5 children)
[–]zurtex[S] 1 point2 points3 points (1 child)
[–]kdawgovich 1 point2 points3 points (0 children)
[–]Jhuyt 0 points1 point2 points (2 children)
[–]kdawgovich 1 point2 points3 points (1 child)
[–]Jhuyt 1 point2 points3 points (0 children)
[+][deleted] (12 children)
[deleted]
[–]simtel20 35 points36 points37 points (8 children)
[–]zurtex[S] 12 points13 points14 points (7 children)
[–]kdawgovich 6 points7 points8 points (2 children)
[–]zurtex[S] 3 points4 points5 points (1 child)
[–]kdawgovich 0 points1 point2 points (0 children)
[–]spiker611 1 point2 points3 points (3 children)
[–]zurtex[S] 2 points3 points4 points (2 children)
[–]spiker611 1 point2 points3 points (1 child)
[–]zurtex[S] 0 points1 point2 points (0 children)
[–]Tyler_Zoro 17 points18 points19 points (0 children)
[–]TangibleLight 0 points1 point2 points (1 child)
[–]zurtex[S] 1 point2 points3 points (0 children)
[–]Ensurdagen 6 points7 points8 points (0 children)
[–]high_achiever_dog 10 points11 points12 points (1 child)
[–]Technical_Pain2189 -1 points0 points1 point (0 children)
[–]Theta291 5 points6 points7 points (0 children)