* operator--as in *args?

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

    #1

    * operator--as in *args?

    Hi,

    I can't find any documentation for the * operator when applied in
    front of a name. Is it a pointer?

    What about the @ operator?

    Are there python names for these operators that would make searching
    for documentation on them more fruitful?

    Thanks

  • Dustan

    #2
    Re: * operator--as in *args?

    On Mar 18, 3:53 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
    Hi,
    >
    I can't find any documentation for the * operator when applied in
    front of a name. Is it a pointer?
    >
    What about the @ operator?
    >
    Are there python names for these operators that would make searching
    for documentation on them more fruitful?
    >
    Thanks
    For the *star, see apply here:


    For example:
    aFunction(*(1,2 ,3))
    is equivalent to:
    aFunction(1,2,3 )

    For the @ symbol, I assume you mean function decorators:


    For example:
    @someDecorator
    def someFunc(): pass
    is equivalent to:
    def someFunc(): pass
    someFunc = someDecorator(s omeFunc)

    Comment

    • 7stud

      #3
      Re: * operator--as in *args?

      On Mar 18, 3:40 pm, "Dustan" <DustanGro...@g mail.comwrote:
      For example:
      aFunction(*(1,2 ,3))
      is equivalent to:
      aFunction(1,2,3 )
      >
      That's the context I've seen it in, but written like this:

      someFunc(*args)

      I played around with it a little bit, and it appears the * operator
      unpacks a list, tuple, or dictionary so that each element of the
      container gets assigned to a different parameter variable. Although
      with a dictionary, only the keys appear to be assigned to the
      parameter variables, e.g.:

      def g(a,b,c):
      print a, b, c

      dict = {"x":10, "y":20, "z":30}
      g(*dict)

      Is that right?



      Comment

      • Alex Martelli

        #4
        Re: * operator--as in *args?

        7stud <bbxx789_05ss@y ahoo.comwrote:
        On Mar 18, 3:40 pm, "Dustan" <DustanGro...@g mail.comwrote:
        For example:
        aFunction(*(1,2 ,3))
        is equivalent to:
        aFunction(1,2,3 )
        >
        That's the context I've seen it in, but written like this:
        >
        someFunc(*args)
        >
        I played around with it a little bit, and it appears the * operator
        unpacks a list, tuple, or dictionary so that each element of the
        container gets assigned to a different parameter variable. Although
        with a dictionary, only the keys appear to be assigned to the
        parameter variables, e.g.:
        >
        def g(a,b,c):
        print a, b, c
        >
        dict = {"x":10, "y":20, "z":30}
        g(*dict)
        >
        Is that right?
        As far as it goes, yes. More generally, with any iterable x, the *x
        construct in function call will pass as positional arguments exactly
        those items which (e.g.) would be printed by the loop:
        for item in x: print x

        [[this applies to iterators, generators, genexps, and any other iterable
        you may care to name -- not just lists, tuples, dicts, but also sets,
        files open for reading [the items are the lines], etc, etc]].


        Alex

        Comment

        • Gabriel Genellina

          #5
          Re: * operator--as in *args?

          En Sun, 18 Mar 2007 18:40:32 -0300, Dustan <DustanGroups@g mail.com>
          escribió:
          >I can't find any documentation for the * operator when applied in
          >front of a name. Is it a pointer?
          >
          For the *star, see apply here:
          http://docs.python.org/lib/non-essen...-in-funcs.html
          Also, you can read this section on the Tutorial:

          or the more technical Language Reference:
          http://docs.python.org/ref/calls.html and

          >Are there python names for these operators that would make searching
          >for documentation on them more fruitful?
          Uhhhm, none that I can think of :(

          --
          Gabriel Genellina

          Comment

          • Gabriel Genellina

            #6
            Re: * operator--as in *args?

            En Sun, 18 Mar 2007 22:21:41 -0300, Alex Martelli <aleax@mac.come scribió:
            7stud <bbxx789_05ss@y ahoo.comwrote:
            >
            >I played around with it a little bit, and it appears the * operator
            >unpacks a list, tuple, or dictionary so that each element of the
            >container gets assigned to a different parameter variable. Although
            >with a dictionary, only the keys appear to be assigned to the
            >parameter variables, e.g.:
            >>
            >def g(a,b,c):
            > print a, b, c
            >>
            >dict = {"x":10, "y":20, "z":30}
            >g(*dict)
            >>
            >Is that right?
            >
            As far as it goes, yes. More generally, with any iterable x, the *x
            construct in function call will pass as positional arguments exactly
            those items which (e.g.) would be printed by the loop:
            for item in x: print x
            >
            [[this applies to iterators, generators, genexps, and any other iterable
            you may care to name -- not just lists, tuples, dicts, but also sets,
            files open for reading [the items are the lines], etc, etc]].
            But the language reference says "sequence", not "iterable"
            (http://docs.python.org/ref/calls.html) and a dictionary is not a
            sequence. With Python 2.1 it was an error; it is not with 2.3 (I can't
            test with 2.2 right now)

            Python 2.1.3 (#35, Apr 8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
            Type "copyright" , "credits" or "license" for more information.
            >>def f(*args, **kw):
            .... print "args",args
            .... print "kw",kw
            ....
            >>d = {"a":1, "b":2, "c":3}
            >>f(**d)
            args ()
            kw {'b': 2, 'c': 3, 'a': 1}
            >>f(*d)
            Traceback (most recent call last):
            File "<stdin>", line 1, in ?
            TypeError: f() argument after * must be a sequence

            If allowing f(*d) is actually the intended behavior, maybe the wording in
            the reference should be updated. If not, f(*d) should still raise an error.

            --
            Gabriel Genellina

            Comment

            • Alex Martelli

              #7
              Re: * operator--as in *args?

              Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
              ...
              As far as it goes, yes. More generally, with any iterable x, the *x
              construct in function call will pass as positional arguments exactly
              those items which (e.g.) would be printed by the loop:
              for item in x: print x

              [[this applies to iterators, generators, genexps, and any other iterable
              you may care to name -- not just lists, tuples, dicts, but also sets,
              files open for reading [the items are the lines], etc, etc]].
              >
              But the language reference says "sequence", not "iterable"
              Yes, Python's docs often say "sequence" where they in fact mean
              "iterable". I count (on all the .tex files under Doc/ in a current SVN
              tree) 854 occurrences of "sequence" versus 251 occurrences of
              "iterable", and I suspect the correct ratio would be rather close to the
              reverse:-).
              If allowing f(*d) is actually the intended behavior, maybe the wording in
              the reference should be updated. If not, f(*d) should still raise an error.
              Patches to the docs will doubtlessly be welcome (though fixing 1 out of
              a suspected 600 or so misuses won't make a huge difference, it's still
              definitely better than nothing:-).


              Alex

              Comment

              • 7stud

                #8
                Re: * operator--as in *args?

                On Mar 18, 7:52 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                wrote:
                def f(*args, **kw):
                >
                ... print "args",args
                ... print "kw",kw
                ...>>d = {"a":1, "b":2, "c":3}
                >f(**d)
                >
                Whoa! **? And applied to a function parameter? Back to the drawing
                board.

                On Mar 18, 7:21 pm, a...@mac.com (Alex Martelli) wrote:
                More generally, with any iterable x, the *x
                construct in function call will pass as positional arguments exactly
                those items which (e.g.) would be printed by the loop:
                for item in x: print x
                >
                Thanks.

                On Mar 18, 7:52 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
                wrote:
                def f(*args, **kw):
                >
                ... print "args",args
                ... print "kw",kw
                ...>>d = {"a":1, "b":2, "c":3}
                >f(**d)
                >
                Whoa! **? And applied to a function parameter name? Back to the
                drawing board.

                The following is what I discovered for anyone else that is interested:

                More on Defining Functions(GvR tutorial, section 4.7)
                --Keyword Argument Lists
                --Arbitrary Argument Lists
                --Unpacking Argument Lists

                1) Applying ** to a function parameter:

                def g(a, b, **xtraArgs):
                print xtraArgs
                g(b=20, c="big", a="hello", d=10)

                #output: {'c': 'big', 'd': 10}

                **xtraArgs will be a dictionary of all arguments sent to the function
                with the syntax "keyword=va lue" if the keyword does not match any of
                the function parameter names.

                2)Applying * to a function parameter:

                def g(a, *xtraArgs):
                print xtraArgs
                g(10, 20, 30, 40)

                #output: (20, 30, 40)

                *xtraArgs will be a tuple containing all arguments sent to the
                function with the syntax "value" that do not match any function
                parameter.

                3) A function parameter with the syntax *xtraArgs1 must come before a
                function parameter with the syntax **xtraArgs2:

                def g(a, b, *xtraArgs1, **xtraArgs2):
                print xtraArgs1
                print xtraArgs2

                g(10, 20, 30, 35, d=40, e="hello")

                #(30, 35)
                #{'e': 'hello', 'd': 40}

                4) Using * to unpack a list, tuple, etc.(any iterable collection--see
                earlier posts):

                def g(a, b, c):
                print a,b,c

                tpl = (1, 2, 3)
                g(*tpl)

                #output: 1 2 3

                5) Using ** to unpack dictionaries:

                def g(a, b, c):
                print a,b,c

                dict = {"b":"world" , "c":"goodby e", "a":"hello" }
                g(**dict)

                #output: hello world goodbye





                Comment

                Working...