__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code)
Dan Sommers
me at privacy.net
Mon Jul 11 08:22:18 EDT 2005
On Mon, 11 Jul 2005 08:34:45 +0200,
Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
> Kay Schluehr wrote:
>> Dan Sommers schrieb:
>>
>>> How about this:
>>>
>>> def __init__(self, self.x, y, self.z):
>>> # self.x, self.z from first and third explicit parameters
>>> do_something_with_y()
>>
>> Can you tell me in which way it is anyhow better than the original
>> proposal
>>
>> def __init__(self, .x, y, .z):
>> # self.x, self.z from first and third explicit parameters
>> do_something_with_y()
>>
>> besides that it is more verbose?
> It is more explicit. Explicit is better than implicit.
> But as with many proposals, this raises consequential questions, for
> example, how "self.x" parameters are handled in other methods, or even
> functions, as __init__ is not special-cased by the parser.
So why limit it to __init__? Bengt Richter's idea of generalizing it is
a good one. Currently, when this method:
def f(self, x, y, z):
pass
is called, Python binds self to a reference to the object, x to the
first argument, y to the second argument, z to and the third. By
extension, hypothetically, this method:
def new_f(self, self.x, y, self.z):
do_something_with_y()
would be semantically identical to:
def new_f(self, __anonymous_argument_1, y, __anonymous_argument_2):
self.x = __anonymous_argument_1
del __anonymous_argument_1 # look: a use case for del! <wink>
self.z = __anonymous_argument_2
del __anonymous_argument_2
do_something_with_y()
It's not too far from the tuple unpacking that happens now:
def g(x, (a, b)):
pass
q = (3, 4)
g(1, q) # inside g, x = 1, a = q[0] = 3, b = q[1] = 4
and it's certainly not less explicit than properties.
Without thinking it all the way through, I suppose these:
def method_1(self, *self.l):
pass
def method_2(self, **self.d):
pass
could act as if they were these:
def method_1(self, *args):
self.l = args
del args
def method_2(self, **kw):
self.d = kw
del kw
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
More information about the Python-list
mailing list