Design principles: no bool arguments
Thomas 'PointedEars' Lahn
PointedEars at web.de
Fri Aug 26 12:09:21 EDT 2011
Ian Kelly wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Both variants work (even in Py3) if you only define [a named argument].
>> You have to define [a keyword argument, e.g. `kwargs'].
>>
>> so that
>>
>> data1.merge_with(data2, True);
>>
>> is a syntax error ("TypeError: merge_with() takes exactly 2 arguments (3
>> given)").
>>
>> IOW, this advantage of Python in readability is not only caused by API
>> definition, but also by how the API is used. It might turn into a
>> disadvantage if key lookups make the code expensive memory- and runtime
>> wise.
>>
>> And you will still have to know the underlying signature to name the
>> argument. Worse, with keyword arguments you *have to* look up the
>> documentation (i. e., it needs to be well-documented as well) to know its
>> name (as the compiler can only tell you "kwargs").
>
> Note though that Python 3 adds actual keyword-only arguments, which
> address all of your points:
>
> class Data:
> def merge_with(self, bar, *, overwrite_duplicates):
> pass
>
>>>> data1.merge_with(data2, True)
> TypeError: merge_with() takes exactly 2 positional arguments (3 given)
>>>> data1.merge_with(data2)
> TypeError: merge_with() needs keyword-only argument overwrite_duplicates
>>>> data1.merge_with(data2, overwrite_duplicates=True)
>>>>
That's good to know. Thanks.
> Of course, in Python 2 that definition would be a syntax error, so you
> can't really take advantage of it if you need compatibility.
ACK.
Regards,
--
PointedEars
Bitte keine Kopien per E-Mail. / Please do not Cc: me.
More information about the Python-list
mailing list