Keyword Arguments

Discussion in 'Python' started by Ryan, Nov 14, 2003.

  1. Ryan

    Ryan Guest

    How can I use the value of a variable to represent a keyword in a function call?

    For example:

    def foo(**kwargs):
    kwargs = **kwargs


    item = "temperature"

    foo(item=25.5)

    I would like the keyword to be the value of item which is temperature.
    Is this possible?

    Thanks
    Ryan
     
    Ryan, Nov 14, 2003
    #1
    1. Advertising

  2. Ryan

    Paul Rubin Guest

    (Ryan) writes:
    > How can I use the value of a variable to represent a keyword in a
    > function call?
    >
    > For example:
    >
    > def foo(**kwargs):
    > kwargs = **kwargs
    >
    > item = "temperature"
    >
    > foo(item=25.5)
    >
    > I would like the keyword to be the value of item which is temperature.
    > Is this possible?


    You mean you want the equivalent of foo(temperature=25.5)? Try:

    args = {item : 25.5}
    foo (**args)
     
    Paul Rubin, Nov 14, 2003
    #2
    1. Advertising

  3. "Ryan" wrote:

    > How can I use the value of a variable to represent a keyword in a function call?
    >
    > For example:
    >
    > def foo(**kwargs):
    > kwargs = **kwargs
    >
    >
    > item = "temperature"
    >
    > foo(item=25.5)
    >
    > I would like the keyword to be the value of item which is temperature.
    > Is this possible?


    foo(**{item: 25.5})

    </F>
     
    Fredrik Lundh, Nov 15, 2003
    #3
  4. Ryan

    ryan scott Guest

    Exactly what I wanted.
    Thanks

    Ryan

    Paul Rubin wrote:
    > (Ryan) writes:
    >
    >>How can I use the value of a variable to represent a keyword in a
    >>function call?
    >>
    >>For example:
    >>
    >>def foo(**kwargs):
    >> kwargs = **kwargs
    >>
    >>item = "temperature"
    >>
    >>foo(item=25.5)
    >>
    >>I would like the keyword to be the value of item which is temperature.
    >>Is this possible?

    >
    >
    > You mean you want the equivalent of foo(temperature=25.5)? Try:
    >
    > args = {item : 25.5}
    > foo (**args)
     
    ryan scott, Nov 15, 2003
    #4
    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:
    427
    David Konerding
    Sep 10, 2003
  2. Edward Diener
    Replies:
    14
    Views:
    4,983
    Josiah Carlson
    Apr 6, 2004
  3. Replies:
    6
    Views:
    459
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

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

Share This Page