[Python-ideas] Augmented assignment syntax for objects.

Brice PARENT contact at brice.xyz
Tue Apr 25 17:15:19 EDT 2017



Le 25/04/17 à 20:16, Mike Miller a écrit :
> Agreed with Steven, although I do find myself a little more annoyed 
> and bothered by a typical init than him I guess.
>
> Even so I didn't think the current proposals went far enough.  To tilt 
> the balance farther, to make it easier, let's go all the way!  Instead 
> of continuing duplication:
>
>> >>> def __init__(self, foo, bar, baz, spam, ham):
>>   self .= foo, bar, baz, spam, ham
>
> or
>
>> # object member assignment
>> self .= foo .= bar .= baz .= spam .= ham
>
> How about?
>
>     def __init__(self, foo, bar, baz, spam, ham):
>       self .= *
>
> The asterisk here used to be reminiscent of argument unpacking (minus 
> self). That would imply a double asterisk for keyword assignment which 
> could be used as well.
>
> Also, I did find the decorator proposal intriguing, though have to say 
> I probably wouldn't bother to use it unless it were a builtin or I had 
> a dozen parameters to deal with.
>
If you *need* a shorter solution, even though I'm not entirely sure 
there's a real need for it, it may be easier to get something like this 
(I think, as there is no new operator involved) :

def __init__(self, *args, **kwargs):
       self.* = *args
       self.** = **kwargs

And I'm sure this could easily find new use cases outside of a constructor.

But, any of these proposals, mine and yours, if you really need this to 
shorten the code writting time or vertical space only, is not a better 
idea than to propose a macro to your IDE editor, or a pull request if 
it's open source. Such a macro would generate automatically those 
long-to-write lines, and maybe implement some code folding if vertical 
space is an issue.

- Brice



More information about the Python-ideas mailing list