[Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations (original) (raw)

Steven D'Aprano steve at pearwood.info
Thu Sep 1 13:30:23 EDT 2016


On Tue, Aug 30, 2016 at 02:20:26PM -0700, Guido van Rossum wrote:

- Whether (given PEP 484's relative success) it's worth adding syntax for variable/attribute annotations.

The PEP makes a good case that it does.

- Whether the keyword-free syntax idea proposed here is best: NAME: TYPE TARGET: TYPE = VALUE

I think so.

That looks like similar to the syntax used by TypeScript:

http://www.typescriptlang.org/docs/handbook/type-inference.html

let zoo: Animal[] = [new Rhino(), new Elephant(), new Snake()];

Some additional thoughts:

Is it okay to declare something as both an instance and class attribute?

class X: spam: int spam: ClassVar[Str] = 'suprise!'

def __init__(self):
    self.spam = 999

I would expect it should be okay.

It is more common in Python circles to talk about class and instance attributes than "variables". Class variable might be okay in a language like Java where classes themselves aren't first-class values, but in Python "class variable" always makes me think it is talking about a variable which is a class, just like a string variable or list variable. Can we have ClassAttr[] instead of ClassVar[]?

Other than that, +1 on the PEP.

-- Steve



More information about the Python-Dev mailing list