Asterisk sign in python

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

    Asterisk sign in python

    Hi Pythoners...

    It seems to me the Asterisk (*) sign in python means many things...

    if a python code line looks like this:
    def__init__(sel f, func, *param):
    bla bla bla...

    and then u have something like this:
    func(*param)

    and then something like this:
    def Wrapper(select, func, param):
    bla bla bla...

    I took this section of code from the 'Python Cookbook' textbook page
    223 about 'Running Functions in the future'...

    I just cant figure out what is the asterisk (*) sign for and what is
    the different between *param and param??

    Many thanks Pythoners...

    Regards
    sarmin


  • John Roth

    #2
    Re: Asterisk sign in python

    "sarmin" <sarmin_kho@yah oo.com> wrote in message
    news:mailman.15 9.1077796055.85 94.python-list@python.org ...[color=blue]
    > Hi Pythoners...
    >
    > It seems to me the Asterisk (*) sign in python means many things...
    >
    > if a python code line looks like this:
    > def__init__(sel f, func, *param):
    > bla bla bla...
    >
    > and then u have something like this:
    > func(*param)
    >
    > and then something like this:
    > def Wrapper(select, func, param):
    > bla bla bla...
    >
    > I took this section of code from the 'Python Cookbook' textbook page
    > 223 about 'Running Functions in the future'...
    >
    > I just cant figure out what is the asterisk (*) sign for and what is
    > the different between *param and param??
    >
    > Many thanks Pythoners...
    >
    > Regards
    > sarmin[/color]

    It's for variable length parameter lists. In a function
    definition, one * is a list of additional positional
    parameters, two **s is a dictionary of additional
    keyword parameters.

    In a function call, one * is a list of additional
    positional parameters, two **s is a dictionary
    of additional keyword parameters.

    John Roth[color=blue]
    >
    >[/color]


    Comment

    • David M. Cooke

      #3
      Re: Asterisk sign in python

      At some point, "John Roth" <newsgroups@jhr othjr.com> wrote:
      [color=blue]
      > "sarmin" <sarmin_kho@yah oo.com> wrote in message
      > news:mailman.15 9.1077796055.85 94.python-list@python.org ...[color=green]
      >> Hi Pythoners...
      >>
      >> It seems to me the Asterisk (*) sign in python means many things...
      >>
      >> if a python code line looks like this:
      >> def__init__(sel f, func, *param):
      >> bla bla bla...
      >>
      >> and then u have something like this:
      >> func(*param)
      >>
      >> and then something like this:
      >> def Wrapper(select, func, param):
      >> bla bla bla...
      >>
      >> I took this section of code from the 'Python Cookbook' textbook page
      >> 223 about 'Running Functions in the future'...
      >>
      >> I just cant figure out what is the asterisk (*) sign for and what is
      >> the different between *param and param??
      >>
      >> Many thanks Pythoners...
      >>
      >> Regards
      >> sarmin[/color]
      >
      > It's for variable length parameter lists. In a function
      > definition, one * is a list of additional positional
      > parameters, two **s is a dictionary of additional
      > keyword parameters.
      >
      > In a function call, one * is a list of additional
      > positional parameters, two **s is a dictionary
      > of additional keyword parameters.[/color]

      Don't forget multiplicaton:

      2 * 2

      and power

      2 ** 2

      :-)

      --
      |>|\/|<
      /--------------------------------------------------------------------------\
      |David M. Cooke
      |cookedm(at)phy sics(dot)mcmast er(dot)ca

      Comment

      • Myles

        #4
        Re: Asterisk sign in python

        Hi Sarmin,
        [color=blue]
        > and then u have something like this:
        > func(*param)[/color]
        [...][color=blue]
        > I just cant figure out what is the asterisk (*) sign for and what is
        > the different between *param and param??[/color]

        The asterisk is used for multiple parameters, which are passed to
        the function as a tuple.

        If your function might be called with a varying number of arguments:

        myfunc(10)
        or
        myfunc(10, 20, 30)
        or
        myfunc(10, "ten", 20, "twenty")

        you could write it as:

        def myfunc(*param):
        print param

        and you would get[color=blue][color=green][color=darkred]
        >>> myfunc(10)[/color][/color][/color]
        (10,)[color=blue][color=green][color=darkred]
        >>> myfunc(10, 20, 30)[/color][/color][/color]
        (10, 20, 30)[color=blue][color=green][color=darkred]
        >>> myfunc(10, "ten", 20, "twenty")[/color][/color][/color]
        (10, 'ten', 20, 'twenty')

        If you had defined your function without the
        asterisk, you could only use a single parameter:

        def myfunc(param):
        print param
        [color=blue][color=green][color=darkred]
        >>> myfunc(10)[/color][/color][/color]
        10[color=blue][color=green][color=darkred]
        >>> myfunc(10, 20, 30)[/color][/color][/color]

        Traceback (most recent call last):
        File "<pyshell#2 7>", line 1, in -toplevel-
        myfunc(10, 20, 30)
        TypeError: myfunc() takes exactly 1 argument (3 given)

        You can have non-optional parameters before the asterisk parameter:

        def myfunc(required , necessary, *optional):
        print "required", required
        print "necessary" , necessary
        print "optional", optional
        [color=blue][color=green][color=darkred]
        >>> myfunc(10, 20, 30, 40)[/color][/color][/color]
        required 10
        necessary 20
        optional (30, 40)[color=blue][color=green][color=darkred]
        >>> myfunc(10)[/color][/color][/color]

        Traceback (most recent call last):
        File "<pyshell#3 5>", line 1, in -toplevel-
        myfunc(10)
        TypeError: myfunc() takes at least 2 arguments (1 given)


        Regards, Myles.

        Comment

        Working...