Dictionary as Keyword Arguments

Discussion in 'Python' started by bfrederi, Dec 12, 2008.

  1. bfrederi

    bfrederi Guest

    I was wondering if I had a dictionary of keywords and values like so:

    keyword_arg_dict = {
    'attribute': 'stone',
    'contents': 'cave people',
    'path': '/path/to/cave',
    'name': 'Ogg's Cave',
    }

    And I had a function that accepted keyword arguments like so:

    make_dwelling(
    attribute='stone',
    contents='cave people',
    path='/path/to/cave',
    name='Ogg's Cave',
    )

    Is there any way I could use my keyword_arg_dict as my keyword args
    for the make_dwelling function, since I am not the creator of the
    make_dwelling function, and need to take that dictionary of key-value
    pairs and turn it into keyword-value arguments for the make_dwelling
    function?
     
    bfrederi, Dec 12, 2008
    #1
    1. Advertising

  2. bfrederi

    Chris Rebert Guest

    On Thu, Dec 11, 2008 at 4:02 PM, bfrederi <> wrote:
    > I was wondering if I had a dictionary of keywords and values like so:
    >
    > keyword_arg_dict = {
    > 'attribute': 'stone',
    > 'contents': 'cave people',
    > 'path': '/path/to/cave',
    > 'name': 'Ogg's Cave',
    > }
    >
    > And I had a function that accepted keyword arguments like so:
    >
    > make_dwelling(
    > attribute='stone',
    > contents='cave people',
    > path='/path/to/cave',
    > name='Ogg's Cave',
    > )
    >
    > Is there any way I could use my keyword_arg_dict as my keyword args
    > for the make_dwelling function, since I am not the creator of the
    > make_dwelling function, and need to take that dictionary of key-value
    > pairs and turn it into keyword-value arguments for the make_dwelling
    > function?


    make_dwelling(**keyword_arg_dict)

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Chris Rebert, Dec 12, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Konerding
    Replies:
    0
    Views:
    425
    David Konerding
    Sep 10, 2003
  2. Edward Diener
    Replies:
    14
    Views:
    4,980
    Josiah Carlson
    Apr 6, 2004
  3. Replies:
    6
    Views:
    458
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    356
  5. Peter Motzfeldt
    Replies:
    1
    Views:
    152
Loading...

Share This Page