473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

trapping errors in function call syntax


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

Feb 13 '06 #1
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
Feb 13 '06 #2
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>

Feb 13 '06 #3
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
Feb 13 '06 #4
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

Feb 13 '06 #5
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>

Feb 13 '06 #6
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.
Feb 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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...
13
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...
9
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 |...
1
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...
0
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$) { } ...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.