what does **kw mean?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zslevi@gmail.com

    #1

    what does **kw mean?

    I've been reading the following example, and couldn't figure out, what
    **kw mean. (It's an empty dictionary, but what's the semantics):


    def wrap(method):
    def wrapped(self, *args, **kw):
    print "begin"
    method(self, *args, **kw)
    print "end"
    return wrapped


    class Test(object):
    def method(self, name):
    print "method(%r) " % name

    t = Test()
    t.method("pure" )
    Test.method = wrap(Test.metho d)
    t.method("wrapp ed")

  • Shane Geiger

    #2
    Re: what does **kw mean?

    Does this help?

    def foobar(first_na me,last_name, *args, **kwargs):
    print first_name
    print last_name
    print "Tuple:",ar gs
    print "Dict:",kwa rgs

    x = "has"
    y = "demonstrat ed"
    foobar('Shane', 'Geiger', x, y, adjective='usef ul', thing='PYTHON trick
    known as extended call syntax', adverb='wonderf ully')




    zslevi@gmail.co m wrote:
    I've been reading the following example, and couldn't figure out, what
    **kw mean. (It's an empty dictionary, but what's the semantics):
    >
    >
    def wrap(method):
    def wrapped(self, *args, **kw):
    print "begin"
    method(self, *args, **kw)
    print "end"
    return wrapped
    >
    >
    class Test(object):
    def method(self, name):
    print "method(%r) " % name
    >
    t = Test()
    t.method("pure" )
    Test.method = wrap(Test.metho d)
    t.method("wrapp ed")
    >
    >

    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    sgeiger@ncee.ne t | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy

    Comment

    • Bruno Desthuilliers

      #3
      Re: what does **kw mean?

      zslevi@gmail.co m a écrit :
      I've been reading the following example, and couldn't figure out, what
      **kw mean. (It's an empty dictionary, but what's the semantics):
      Keyword varargs. And FWIW, *args is for positional varargs.

      Comment

      • Lie

        #4
        Re: what does **kw mean?

        On Jan 11, 4:38 pm, "zsl...@gmail.c om" <levili...@gmai l.comwrote:
        I've been reading the following example, and couldn't figure out, what
        **kw mean. (It's an empty dictionary, but what's the semantics):
        It's a keyword argument. It's some kind of repository for arguments
        that aren't recognized.

        If you have function like this:
        def func(a, *args, *kw):
        print a
        print args
        print kw

        and you call the functin like this:
        func('value A', 'value B', 'value C', argumentA = 'value D', argumentB
        = 'value D')
        the extra arguments would normally raise an error, but with the * and
        **, Python would:
        - assign 'value B' and 'value C' to args
        - assign 'argumentA':'va lue D' and 'argumentB':'va lue E' to kw

        so if you run the function, it will output:
        ####
        value A
        ('value B', 'value C')
        {'argumentB': 'value E', 'argumentA': 'value D'}
        ####

        this args and kw can be accessed like a tuple and dictionary
        respectively

        See '4.7.2 Keyword Arguments' and '4.7.3 Arbitrary Argument Lists' on
        Python Help File

        Comment

        • zslevi@gmail.com

          #5
          Re: what does **kw mean?

          On Jan 11, 12:24 pm, Lie <Lie.1...@gmail .comwrote:
          On Jan 11, 4:38 pm, "zsl...@gmail.c om" <levili...@gmai l.comwrote:
          >
          I've been reading the following example, and couldn't figure out, what
          **kw mean. (It's an empty dictionary, but what's the semantics):
          >
          It's a keyword argument. It's some kind of repository for arguments
          that aren't recognized.
          >
          If you have function like this:
          def func(a, *args, *kw):
          print a
          print args
          print kw
          >
          and you call the functin like this:
          func('value A', 'value B', 'value C', argumentA = 'value D', argumentB
          = 'value D')
          the extra arguments would normally raise an error, but with the * and
          **, Python would:
          - assign 'value B' and 'value C' to args
          - assign 'argumentA':'va lue D' and 'argumentB':'va lue E' to kw
          >
          so if you run the function, it will output:
          ####
          value A
          ('value B', 'value C')
          {'argumentB': 'value E', 'argumentA': 'value D'}
          ####
          >
          this args and kw can be accessed like a tuple and dictionary
          respectively
          >
          See '4.7.2 Keyword Arguments' and '4.7.3 Arbitrary Argument Lists' on
          Python Help File
          Thanks!

          Comment

          Working...