Default annotations for variables
Kirill Balunov
kirillbalunov at gmail.com
Wed Dec 27 06:19:09 EST 2017
2017-12-27 13:54 GMT+03:00 Chris Angelico <rosuav at gmail.com>:
> This won't work. When you say "a.__type__", it means "take the *value*
> referenced by a, and look up its __type__ attribute". So it's
> equivalent to writing:
>
> (11).__type__
> (12).__type__
>
Thank you for clarification, I understand that names don't have attributes
:). But I did not find the right words to describe the idea.
> To query something about the *variable*, you have to look at its
> enclosing namespace. If these are at top level, you'd be looking at
> the module-level __annotations__ dictionary. That's also where
> x.__type__ would be - it's actually stored in __annotations__["x"].
>
Here I was a bit knocked down by the IPython console. Strangely, but the `
__annotations__` is not initialized to a an empty dict when you start it,
while in Python console it is.
> There is definitely room to ask the question "can we get default
> annotations for any global or class-level name that gets assigned to
> without an annotation?". I fully expect that thread to be one of those
> gigantic ones, but have fun :)
>
Yes this is exactly what I was about to ask :)
With kind regards, -gdg
More information about the Python-list
mailing list