[Python-ideas] Augmented assignment syntax for objects.

Nick Coghlan ncoghlan at gmail.com
Sat Apr 29 22:25:06 EDT 2017


On 29 April 2017 at 03:00, Mike Miller <python-ideas at mgmiller.net> wrote:
> On 2017-04-28 06:07, Nick Coghlan wrote:
>> 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):
>
Putting any questions of aesthetics aside, the main argument in favour
of using a base class and __init_subclass__ for autoclass definitions
would be the fact that further subclasses would automatically get the
magic method construction executed, rather than requiring that the
class decorator be repeated on the subclass.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list