Hello:
Suppose I write a function that I want to be called
with ONLY keyword argumnts, how do I raise an
exception should the function get called with
what look like position-specfic arguments?
Any help would be appreciated.
Avi Kak 6 1276
On Mon, 2006-02-13 at 14:13, Avi Kak wrote: Hello:
Suppose I write a function that I want to be called with ONLY keyword argumnts, how do I raise an exception should the function get called with what look like position-specfic arguments?
Any help would be appreciated.
Something like this should do the trick:
def f(*args, **kwargs):
if args: raise TypeError, "Please use keyword arguments."
...
-Carsten
Avi Kak wrote: Suppose I write a function that I want to be called with ONLY keyword argumnts, how do I raise an exception should the function get called with what look like position-specfic arguments?
here's one way to do it: def func(*args, **kw):
.... def myrealfunc(a=1, b=2, c=3):
.... print a, b, c
.... if args:
.... raise TypeError("invalid call")
.... return myrealfunc(**kw)
.... func(1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in func
TypeError: invalid call func(a=3)
3 2 3 func()
1 2 3
here's another one:
def func(dummy=None, a=1, b=2, c=3):
.... if dummy is not None:
.... raise TypeError("invalid call")
.... print a, b, c
(but this is easier to trick).
hope this helps!
</F>
While we're at it... Can someone point me to either an old post, or
documentation about tuple expansion with * ? I recently saw it used
and was shocked as i had no clue what it really did. I didn't know it
could be used outside of function definitions.
On 2/13/06, Fredrik Lundh <fr*****@pythonware.com> wrote: Avi Kak wrote:
Suppose I write a function that I want to be called with ONLY keyword argumnts, how do I raise an exception should the function get called with what look like position-specfic arguments?
here's one way to do it:
def func(*args, **kw): ... def myrealfunc(a=1, b=2, c=3): ... print a, b, c ... if args: ... raise TypeError("invalid call") ... return myrealfunc(**kw) ... func(1) Traceback (most recent call last): File "<stdin>", line 1, in ? File "<stdin>", line 5, in func TypeError: invalid call func(a=3) 3 2 3 func() 1 2 3
here's another one: def func(dummy=None, a=1, b=2, c=3):
... if dummy is not None: ... raise TypeError("invalid call") ... print a, b, c
(but this is easier to trick).
hope this helps!
</F> -- http://mail.python.org/mailman/listinfo/python-list
--
Andrew Gwozdziewycz <ap****@gmail.com> http://ihadagreatview.org http://plasticandroid.org
Avi Kak wrote: Hello:
Suppose I write a function that I want to be called with ONLY keyword argumnts, how do I raise an exception should the function get called with what look like position-specfic arguments?
Any help would be appreciated.
Say you want the signature to be foo(a, b, c) with no positional
arguments allowed. You can do this:
def foo(*args, **kwargs):
a = kwargs.pop('a')
b = kwargs.pop('b')
c = kwargs.pop('c')
if args or kwargs:
raise TypeError("foo takes only keyword arguments: foo(a,b,c)")
# ...
Or something similar.
-tim
Andrew Gwozdziewycz wrote: While we're at it... Can someone point me to either an old post, or documentation about tuple expansion with * ? I recently saw it used and was shocked as i had no clue what it really did. I didn't know it could be used outside of function definitions.
the reference page is here: http://docs.python.org/ref/calls.html
</F>
Fredrik Lundh wrote: Avi Kak wrote: Suppose I write a function that I want to be called with ONLY keyword argumnts, how do I raise an exception should the function get called with what look like position-specfic arguments?
here's one way to do it:
def func(*args, **kw): ... def myrealfunc(a=1, b=2, c=3): ... print a, b, c ... if args: ... raise TypeError("invalid call") ... return myrealfunc(**kw) ...
If you aren't particular about the message then you can do without the args
argument and just define it with **kw: def func(**kw):
def myrealfunc(a=1, b=2, c=3):
print a, b, c
return myrealfunc(**kw)
func(1)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in -toplevel-
func(1)
TypeError: func() takes exactly 0 arguments (1 given) func(a=3)
3 2 3
and if you are particular about the message perhaps submitting a patch to
generate a more appropriate message would be a good idea. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Frost |
last post by:
Please help
I don't know if this is possible but what I would really like to do is
to use On Error Goto to capture the code that is being executed when
an error occurs.
Any help would be much...
|
by: Thelma Lubkin |
last post by:
I use code extensively; I probably overuse it. But I've been using
error trapping very sparingly, and now I've been trapped by that.
A form that works for me on the system I'm using, apparently...
|
by: 47computers |
last post by:
Pretty new to PHP, I recently started learning about error trapping.
As of right now, I include the following into a page in my website:
-------BEGIN PASTE--------
error_reporting(E_ERROR |...
|
by: asearle |
last post by:
I am a bit new to Perl and am having trouble trapping errors in script ...
However, I think that it is just simple syntax issues and hope that someone can point me towards a good HOWTO which might...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |