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?
On Mon, 2006-02-13 at 14:13, Avi Kak wrote:[color=blue]
> 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.[/color]
Something like this should do the trick:
def f(*args, **kwargs):
if args: raise TypeError, "Please use keyword arguments."
...
Avi Kak wrote:
[color=blue]
> 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?[/color]
here's one way to do it:
[color=blue][color=green][color=darkred]
>>> def func(*args, **kw):[/color][/color][/color]
.... def myrealfunc(a=1, b=2, c=3):
.... print a, b, c
.... if args:
.... raise TypeError("inva lid call")
.... return myrealfunc(**kw )
....[color=blue][color=green][color=darkred]
>>> func(1)[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in func
TypeError: invalid call[color=blue][color=green][color=darkred]
>>> func(a=3)[/color][/color][/color]
3 2 3[color=blue][color=green][color=darkred]
>>> func()[/color][/color][/color]
1 2 3
here's another one:
[color=blue][color=green][color=darkred]
>>> def func(dummy=None , a=1, b=2, c=3):[/color][/color][/color]
.... if dummy is not None:
.... raise TypeError("inva lid call")
.... print a, b, c
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 <fredrik@python ware.com> wrote:[color=blue]
> Avi Kak wrote:
>[color=green]
> > 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?[/color]
>
> here's one way to do it:
>[color=green][color=darkred]
> >>> def func(*args, **kw):[/color][/color]
> ... def myrealfunc(a=1, b=2, c=3):
> ... print a, b, c
> ... if args:
> ... raise TypeError("inva lid call")
> ... return myrealfunc(**kw )
> ...[color=green][color=darkred]
> >>> func(1)[/color][/color]
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<stdin>", line 5, in func
> TypeError: invalid call[color=green][color=darkred]
> >>> func(a=3)[/color][/color]
> 3 2 3[color=green][color=darkred]
> >>> func()[/color][/color]
> 1 2 3
>
> here's another one:
>[color=green][color=darkred]
> >>> def func(dummy=None , a=1, b=2, c=3):[/color][/color]
> ... if dummy is not None:
> ... raise TypeError("inva lid call")
> ... print a, b, c
>
> (but this is easier to trick).
>
> hope this helps!
>
> </F>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>[/color]
Avi Kak wrote:[color=blue]
> 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.[/color]
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)")
# ...
Andrew Gwozdziewycz wrote:
[color=blue]
> 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.[/color]
Fredrik Lundh wrote:[color=blue]
> Avi Kak wrote:[color=green]
>> 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?[/color]
>
> here's one way to do it:
>[color=green][color=darkred]
>>>> def func(*args, **kw):[/color][/color]
> ... def myrealfunc(a=1, b=2, c=3):
> ... print a, b, c
> ... if args:
> ... raise TypeError("inva lid call")
> ... return myrealfunc(**kw )
> ...
>[/color]
If you aren't particular about the message then you can do without the args
argument and just define it with **kw:
[color=blue][color=green][color=darkred]
>>> def func(**kw):[/color][/color][/color]
def myrealfunc(a=1, b=2, c=3):
print a, b, c
return myrealfunc(**kw )
Comment