[Python-ideas] Augmented assignment syntax for objects.

MRAB python at mrabarnett.plus.com
Wed Apr 26 14:11:31 EDT 2017


On 2017-04-26 18:46, Mike Miller wrote:
> 
> On 2017-04-26 04:12, Brice PARENT wrote:
>> Why not simply do this :
>>
>> class MyClass:
>>     def _set_multiple(self, **kwargs):
>>         for key, value in kwargs.items():
>>             setattr(self, key, value)
>>
>>     def __init__(self, a, b, c):
>>         self._set_multiple(a=a, b=b, c=c)
> 
> If the goal is to not have to type out argument names three times (in DRY
> fashion), this doesn't quite fit the bill.
> 
An alternative is to use a special comment, for example:

class MyClass:
     def __init__(self, a, b, c):
         #> a, b, c

And then use, say, a Python script to expand it to:

class MyClass:
     def __init__(self, a, b, c):
         #> a, b, c
         self.a = a
         self.b = b
         self.c = c
         #<

The Python script could be called from the editor.

If you want to add or remove an argument, edit the comment and then run 
the script again. (Hopefully it'll be smart enough to make the necessary 
changes).


More information about the Python-ideas mailing list