__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code)

Bengt Richter bokr at oz.net
Sun Jul 10 22:32:09 EDT 2005


On 10 Jul 2005 16:07:40 -0700, Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

>Dan Sommers <me at privacy.net> writes:
>>     def __init__(self, self.x, y, self.z):
>>         # self.x, self.z from first and third explicit parameters
>>         do_something_with_y()
>
>Hey, I like that.

Me too. I liked the leading _, but on second thought it is a weird language change
re names in a special context. Probably not so good.

OTOH, I see in the above a generalizable principle of formal parameter expressions as
automatic assignment targets at the beginning of function execution that could allow
anything normally allowed locally on the left of an assignment. This is contrived,
but illustrates:

    def setem(key, adict[key]): pass # note that assignment is left-right,
                                     # so key is available for adict[key]
                                     # and adict is global here
    adict = {}
    setem('k', 'value')
    adict -> {'k':'value'}

Regards,
Bengt Richter



More information about the Python-list mailing list