[Python-ideas] Augmented assignment syntax for objects.

Mike Miller python-ideas at mgmiller.net
Fri Apr 28 13:00:56 EDT 2017


On 2017-04-28 06:07, Nick Coghlan wrote:
> …the root cause (writing imperative initialisers is still
> part of the baseline recommendation for writing classes, and we don't
> offer any supporting infrastructure for avoiding that directly in the
> standard library)
>
> For a *lot* of classes, what we want to be able to define is:
>
> - a set of data fields
> - a basic initialiser to set those fields by name
> - a repr based on those fields (or a subset)
> - equality comparisons based on those fields (or a subset)
> - ordering comparisons based on those fields (or a subset)
> - conversion to a JSON-friendly dict
> - pickling support
> - copying support

Nice!  I've implemented a higher-level base class in a few projects that does 
just this.  Things like pickling and JSON I might want to put into "mixins", but 
yes the majority of these would be nice to have implemented by default 
"batteries included" style.  I prefer a base class to decorators since they seem 
to stay out of the way more:

     class Vehicle(BaseObject, Picklable):
         …

That still leaves the problem of how to define instance attributes concisely. 
Perhaps a "defaults" member or marker, or one of the other examples in the 
larger thread would do.


More information about the Python-ideas mailing list