On Sun, Apr 18, 2010 at 4:23 PM, Xavier Ho <span dir="ltr"><<a href="mailto:contact@xavierho.com">contact@xavierho.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
G'day Pythoneers,<br><br>I ran into a strange problem today: why does Python not allow default paranmeters for packed arguments in a function def?<br><span class="Apple-style-span" style="font-family: 'courier new', monospace; ">>>> def t(a, *b = (3, 4)):</span><br>
<span style="font-family:courier new,monospace"> File "<input>", line 1</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace"> def t(a, *b = (3, 4)):</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace"> ^</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">SyntaxError: invalid syntax</span><br><br>What was the rationale behind this design?<br></blockquote><div><br></div><div>Why doesn't Python allow this? Because it just doesn't. I'm not sure there's so much as a rationale why its not that way, but there's no rationale for it to BE that way.</div>
<div> </div><div>A default argument is an argument which is optional, but has a default value. *args and **kwargs are unknown optional additional arguments. I'm not sure how you can logically combine the two premises. They are sort of mutually exclusive, the only similarity is both are 'optional'. They're just entirely different ways to achieve Optionality.</div>
<div><br></div><div>And since no one's ever wanted it that I can ever remember, it seems to be a rare sort of use-case, which you could probably easily satisfy by:</div><div><br></div><div>def t(a, *b):</div><div> if not b:</div>
<div> b = (2,3)</div><div> ...</div><div><br></div><div>Its a similar pattern to using None as the value of an optional argument as a sentinel, to then assign the real 'default' value to it once in the function (this pattern is used when said real default value is mutable, of course).</div>
<div><br></div><div><br></div></div><div name="mailplane_signature">--S</div>