Aviv Roth
Forcing keyword arguments (kwargs) in Python
If you use Python, I’d highly recommend joining realpython.com just for the emails. They include all sorts of tips that I personally would never uncover otherwise.
Here’s a good one from today’s email. You can force a function’s arguments (args, which can be passed without a key) to be keyword arguments (kwargs, which must be passed in the format key=value). So for example:
>>> def func(a, *, b, c):
... print(a)
... print(b)
... print(c)
...
>>> func(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: func() takes 1 positional argument but 3 were given
>>> fu(1,b=2,c=3)
1
2
3
>>>
Pretty neat.
Recent Comments
- JPEREZGIL on REST calls in .NET (C#) over SSL (HTTPS)
- Reivaj810 on REST calls in .NET (C#) over SSL (HTTPS)
- Juancastein on Installing SharePoint 2013 on Windows Server 2012 R2 Preview
- Juancastein on Installing SharePoint 2013 on Windows Server 2012 R2 Preview
- Arjen Holterman on REST calls in .NET (C#) over SSL (HTTPS)
Categories