To avoid the obvious bugs, I'd like to prevent the use of positional arguments with some functions. Is there any way to achieve that?
-
2Why do it? We are all consenting adults here, right? – ktdrv Oct 02 '11 at 06:08
-
1The answer is actually really simple: don't. I know, you might think that there is an awesome use case which will solve something brilliantly, but trust me, you are working against the fundamental philosophy of the language -- we are all consenting adults. It will only cause sorrow to pursue such a course. – cwallenpoole Oct 02 '11 at 06:56
-
This sounds like a silly thing to _enforce_. However, if you like, you can get into the habit of specifying positional arg by name at function call time. That way if name of the positional arg changes, callers would get a TypeError (is this the kind of obvious bug you're trying to avoid?). – wim Oct 02 '11 at 07:54
-
7It's not that silly to enforce. Some functions are better off with keyword-only args: `list.sort(reverse=True)` is one example. This allows a later version of the API to rearrange the arguments (as Python 3 did). If argument order is not part of the API, code using positional arguments may break if with later versions of the library in question. Such errors should never pass silently. – Petr Viktorin Oct 02 '11 at 15:13
3 Answers
Only Python 3 can do it properly (and you used the python3 tag, so it's fine):
def function(*, x, y, z):
print(x,y,z)
using **kwargs will let the user input any argument unless you check later. Also, it will hide the real arguments names from introspection.
**kwargs is not the answer for this problem.
Testing the program:
>>> function(1,2,3)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
function(1,2,3)
TypeError: function() takes exactly 0 positional arguments (3 given)
>>> function(x=1, y=2, z=3)
1 2 3
- 30,604
- 10
- 86
- 109
You could define a decorator that, using introspection, causes an error if the function that it decorates uses any positional arguments. This allows you to prevent the use of positional arguments with some functions, while allowing you to define those functions as you wish.
As an example:
def kwargs_only(f):
def new_f(**kwargs):
return f(**kwargs)
return new_f
To use it:
@kwargs_only
def abc(a, b, c): return a + b + c
You cannot use it thus (type error):
abc(1,2,3)
You can use it thus:
abc(a=1,b=2,c=3)
A more robust solution would use the decorator module.
Disclaimer: late night answers are not guaranteed!
- 974
- 7
- 16
Yes, just use the **kwargs construct and only read your parameters from there.
def my_function(**kwargs):
for key, value in kwargs.iteritems():
print ("%s = %s" % (key, value))
my_function(a="test", b="string")
- 46,695
- 12
- 79
- 117
-
Hey downvoters: can tell me how this doesn't satisfy the OP's requirements? Exact words: "prevent the use of positional arguments". Nothing else was specified. – Chris Eberle Oct 08 '11 at 18:08