[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484
Chris Angelico
rosuav at gmail.com
Wed Aug 3 11:47:47 EDT 2016
On Thu, Aug 4, 2016 at 1:40 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Wed, Aug 3, 2016 at 11:31 AM, Joao S. O. Bueno <jsbueno at python.org.br> wrote:
>> An __annotations__ dict straight in the class itself? I think that is
>> the obvious thing - although I did not see it in the above messages.
>> (A s I said, I may have missed its mention)
>
> It was mentioned that var annotations would be evaluated and
> discarded, but I agree that this is less than ideal. I agree that it
> is better to store var annotations in the namespace that they appear
> in. Maybe something like this:
>
> a: int = 0
> b: List[float] = []
>
> would result in a namespace
>
> {
> 'a': 0,
> 'b': [],
> '__var_annotations__': {
> 'a': int,
> 'b': List[float],
> },
So... the maligned statement "a: int" would actually mean
"__var_annotations__['a'] = int", which is a perfectly valid
executable statement. This might solve the problem?
Question, though: Does __var_annotations__ always exist (as a
potentially empty dict), or is it created on first use? A function's
__annotations__ is always present, but functions have lots of
attributes that we normally don't care about. Polluting every single
namespace seems wasteful; magically creating a new local variable when
you first hit an annotated 'declaration' seems odd.
ChrisA
More information about the Python-ideas
mailing list