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

Koos Zevenhoven k7hoven at gmail.com
Thu Sep 1 09:11:05 EDT 2016


On Wed, Aug 31, 2016 at 12:20 AM, Guido van Rossum <guido at python.org> wrote:

I'm happy to present PEP 526 for your collective review: https://www.python.org/dev/peps/pep-0526/ (HTML) https://github.com/python/peps/blob/master/pep-0526.txt (source)

There's also an implementation ready: https://github.com/ilevkivskyi/cpython/tree/pep-526 I don't want to post the full text here but I encourage feedback on the high-order ideas, including but not limited to - Whether (given PEP 484's relative success) it's worth adding syntax for variable/attribute annotations.

While a large amount of Python programmers may not be interested in type hinting local variables inside functions, I can see other potential benefits in this.

When I start sketching a new class, I'm often tempted to write down the names of the attributes first, before starting to implement __init__. Sometimes I even write temporary comments for this purpose. This syntax would naturally provide a way to sketch the list of attributes. Yes, there is already slots, but I'm not sure that is a good example of readability.

Also, when reading code, it may be hard to tell which (instance) attributes the class implements. To have these listed in the beginning of the class could therefore improve the readability.

In this light, I'm not sure it's a good idea to allow attribute type hints inside methods.

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

I wonder if this would be better:

def NAME: TYPE def NAME: TYPE = VALUE

Maybe it's just me, but I've always thought 'def' is Python's least logically used keyword. It seems to come from 'define', but what is it about 'define' that makes it relate to functions only. Adding an optional 'def' for other variables might even be a tiny bit of added consistency.

Note that we could then also have this:

def NAME

Which would, again for readability (see above), be a way to express that "there is an instance variable called X, but no type hint for now". I can't think of a good way to do this with the keyword-free version for people that don't use type hints.

And then there could also be a simple decorator like @slotted_attributes that automatically generates "slots" from the annotations.

-- Koos

Note that there's an extensive list of rejected ideas in the PEP; please be so kind to read it before posting here: https://www.python.org/dev/peps/pep-0526/#rejected-proposals-and-things-left-out-for-now

-- --Guido van Rossum (python.org/~guido)


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/k7hoven%40gmail.com

--



More information about the Python-Dev mailing list