[Python-ideas] Augmented assignment syntax for objects.

Ivan Levkivskyi levkivskyi at gmail.com
Fri Apr 28 19:51:04 EDT 2017


typing.NamedTuple was already mentioned in this discussion, I just would
like to add few comments:

1. I think that changing Python syntax to support declarative classes is
not a realistic option in nearby future.
In contrast, there is an existing syntax change - variable annotations -
that can be actively experimented with.
Variable annotations are designed to play well with metaclass machinery,
for example, they will respect
__prepare__ method, if it injects __annotations__ as a custom mapping, etc.

2. Possible strategy for declarative classes could be developing
independent *composable* mixins/traits:

  @comparable
  @pickleable
  class MyClass:
      x: int
      y: int

or

  class MyClass(Comparable, Pickleable):
      x: int
      y: int

The problem with the second approach is that it can lead to metaclass
conflicts. A possible solution will
be to provide a standard metaclass for mixins in stdlib.

3. Composability could be a problem if only normal variable assignments are
used: after one mixin transforms the class,
the original declarations are lost. Simple possible solution would be to
agree that mixins should *not* modify
__annotations__, only __dict__. Currently, PEP 484 and PEP 526 specify that
preferable use of annotations is type
declarations. However, I think this can be generalized to just
declarations. For example, by providing a special typing
construct that will allow additional info apart from type:

  def large(val):
      return val > 42

  @comparable
  @pickleable
  class MyClass
      x: Spec(int, validator=large)
      y: Spec(List[int], default_factory=list)


--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170429/109b8a11/attachment.html>


More information about the Python-ideas mailing list