trapping errors in function call syntax

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Avi Kak

    #1

    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

  • Carsten Haese

    #2
    Re: trapping errors in function call syntax

    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."
    ...

    -Carsten


    Comment

    • Fredrik Lundh

      #3
      Re: trapping errors in function call syntax

      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

      (but this is easier to trick).

      hope this helps!

      </F>



      Comment

      • Andrew Gwozdziewycz

        #4
        Re: trapping errors in function call syntax

        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]


        --
        Andrew Gwozdziewycz <apgwoz@gmail.c om>


        Comment

        • Tim Hochberg

          #5
          Re: trapping errors in function call syntax

          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)")
          # ...


          Or something similar.

          -tim

          Comment

          • Fredrik Lundh

            #6
            Re: trapping errors in function call syntax

            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]

            the reference page is here:



            </F>



            Comment

            • Duncan Booth

              #7
              Re: trapping errors in function call syntax

              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 )

              [color=blue][color=green][color=darkred]
              >>> func(1)[/color][/color][/color]

              Traceback (most recent call last):
              File "<pyshell#1 4>", line 1, in -toplevel-
              func(1)
              TypeError: func() takes exactly 0 arguments (1 given)[color=blue][color=green][color=darkred]
              >>> func(a=3)[/color][/color][/color]
              3 2 3[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]

              and if you are particular about the message perhaps submitting a patch to
              generate a more appropriate message would be a good idea.

              Comment

              Working...