Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>True, <em>most programming languages</em> make parameter order part of the function call contract, but this doesn't <em>need</em> to be so. Why would it? My understanding of the question is, then, if Python is any different to other programming languages in this respect. In addition to other good answers for Python 2, please consider the following:</p> <pre><code>__named_only_start = object() def info(param1,param2,param3,_p=__named_only_start,spacing=10,collapse=1): if _p is not __named_only_start: raise TypeError("info() takes at most 3 positional arguments") return str(param1+param2+param3) +"-"+ str(spacing) +"-"+ str(collapse) </code></pre> <p>The only way a caller would be able to provide arguments <code>spacing</code> and <code>collapse</code> positionally (without an exception) would be:</p> <pre><code>info(arg1, arg2, arg3, module.__named_only_start, 11, 2) </code></pre> <p>The convention of not using private elements belonging to other modules already is very basic in Python. As with Python itself, this convention for parameters would only be semi-enforced.</p> <p>Otherwise, calls would need to be of the form:</p> <pre><code>info(arg1, arg2, arg3, spacing=11, collapse=2) </code></pre> <p>A call</p> <pre><code>info(arg1, arg2, arg3, 11, 2) </code></pre> <p>would assign value 11 to parameter <code>_p</code> and an exception risen by the function's first instruction.</p> <p>Characteristics:</p> <ul> <li>Parameters before <code>_p=__named_only_start</code> are admitted positionally (or by name).</li> <li>Parameters after <code>_p=__named_only_start</code> must be provided by name only (unless knowledge about the special sentinel object <code>__named_only_start</code> is obtained and used).</li> </ul> <p>Pros:</p> <ul> <li>Parameters are explicit in number and meaning (the later if good names are also chosen, of course).</li> <li>If the sentinel is specified as first parameter, then all arguments need to be specified by name.</li> <li>When calling the function, it's possible to switch to positional mode by using the sentinel object <code>__named_only_start</code> in the corresponding position.</li> <li>A better performance than other alternatives can be anticipated.</li> </ul> <p>Cons:</p> <ul> <li><s>Checking occurs during run-time, not compile-time.</s></li> <li>Use of an extra parameter (though not argument) and an additional check. Small performance degradation respect to regular functions.</li> <li>Functionality is a hack without direct support by the language (see note below).</li> <li>When calling the function, it's possible to switch to positional mode by using the sentinel object <code>__named_only_start</code> in the right position. Yes, this can also be seen as a pro.</li> </ul> <p>Please do keep in mind that this answer is only valid for Python 2. Python 3 implements the similar, but very elegant, language-supported mechanism described in other answers.</p> <p>I've found that when I open my mind and think about it, no question or other's decision seems stupid, dumb, or just silly. Quite on the contrary: I typically learn a lot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload