Dynamic function execution

Discussion in 'Python' started by Andy Wu, Nov 25, 2006.

  1. Andy Wu

    Andy Wu Guest

    Hi guys,

    There's a function I want to use which looks like this:

    def func(seconds = None, minutes = None, hours = None):
    ...

    In my program I can get a string object('seconds', 'minutes', 'hours')
    to specify which parameter to use, the problem is I don't know how to
    call the function.

    Say I have a string 'minutes' and a integer 30, now I need to call the
    func this way: func(minutes = 30), how do I do this?

    I'm sure this is a simple question, but I can't google it out since I
    don't know how to describe it in a short term.

    Thanks,

    Andy Wu
     
    Andy Wu, Nov 25, 2006
    #1
    1. Advertising

  2. Andy Wu wrote:

    > def func(seconds = None, minutes = None, hours = None):
    > ...
    >
    > In my program I can get a string object('seconds', 'minutes', 'hours')
    > to specify which parameter to use, the problem is I don't know how to
    > call the function.
    >
    > Say I have a string 'minutes' and a integer 30, now I need to call the
    > func this way: func(minutes = 30), how do I do this?


    func(**{"minutes": 30})

    </F>
     
    Fredrik Lundh, Nov 25, 2006
    #2
    1. Advertising

  3. Andy Wu wrote:
    > Say I have a string 'minutes' and a integer 30, now I need to call the
    > func this way: func(minutes = 30), how do I do this?


    d={"minutes": 30}
    func(**d)

    This is "extended call syntax". You can read more about this when
    you look up the (deprecated) "apply" function in the manual.

    --Irmen
     
    Irmen de Jong, Nov 25, 2006
    #3
  4. In article <>,
    Fredrik Lundh <> wrote:
    >Andy Wu wrote:
    >
    >> def func(seconds = None, minutes = None, hours = None):
    >> ...
    >>
    >> In my program I can get a string object('seconds', 'minutes', 'hours')
    >> to specify which parameter to use, the problem is I don't know how to
    >> call the function.
    >>
    >> Say I have a string 'minutes' and a integer 30, now I need to call the
    >> func this way: func(minutes = 30), how do I do this?

    >
    > func(**{"minutes": 30})
    >
    ></F>
    >


    Now I'm confused: what's the advantage of

    def func(seconds = None, minutes = None, hours = None):
    print seconds
    print minutes
    print hours

    func(**{"minutes": 30})

    over

    def func(seconds = None, minutes = None, hours = None):
    print seconds
    print minutes
    print hours

    func(minutes = 30)

    ? Or am I missing the point that a better example of what
    Mr. Wu really wants is

    def func(seconds = None, minutes = None, hours = None):
    print seconds
    print minutes
    print hours

    dimension = "minutes"
    func(**{dimension: 30})

    ?
     
    Cameron Laird, Nov 25, 2006
    #4
  5. Andy Wu

    John Machin Guest

    Cameron Laird wrote:
    > In article <>,
    > Fredrik Lundh <> wrote:
    > >Andy Wu wrote:
    > >
    > >> def func(seconds = None, minutes = None, hours = None):
    > >> ...
    > >>
    > >> In my program I can get a string object('seconds', 'minutes', 'hours')
    > >> to specify which parameter to use, the problem is I don't know how to
    > >> call the function.
    > >>
    > >> Say I have a string 'minutes' and a integer 30, now I need to call the
    > >> func this way: func(minutes = 30), how do I do this?

    > >
    > > func(**{"minutes": 30})
    > >
    > ></F>
    > >

    >
    > Now I'm confused: what's the advantage of
    >
    > def func(seconds = None, minutes = None, hours = None):
    > print seconds
    > print minutes
    > print hours
    >
    > func(**{"minutes": 30})
    >
    > over
    >
    > def func(seconds = None, minutes = None, hours = None):
    > print seconds
    > print minutes
    > print hours
    >
    > func(minutes = 30)
    >
    > ? Or am I missing the point that a better example of what
    > Mr. Wu really wants is
    >
    > def func(seconds = None, minutes = None, hours = None):
    > print seconds
    > print minutes
    > print hours
    >
    > dimension = "minutes"
    > func(**{dimension: 30})
    >
    > ?


    Hi Cameron,

    You're on the right track. A better example would have the last two
    lines replaced by:

    # Simulate obtaining data
    argument_name = "minutes"
    argument_value = 30
    # Then ...
    func(**{argument_name: argument_value})

    :)

    Cheers,
    John
     
    John Machin, Nov 26, 2006
    #5
  6. Cameron Laird wrote:

    > ? Or am I missing the point that a better example of what
    > Mr. Wu really wants is
    >
    > def func(seconds = None, minutes = None, hours = None):
    > print seconds
    > print minutes
    > print hours
    >
    > dimension = "minutes"
    > func(**{dimension: 30})


    I assumed that the OP was looking for a mechanism that allowed him to
    use strings for parameter names, not that he wasn't able to replace a
    literal with a variable once he knew what mechanism to use...

    </F>
     
    Fredrik Lundh, Nov 26, 2006
    #6
    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. Kishor

    Dynamic Execution of Function/Proc

    Kishor, Sep 24, 2003, in forum: ASP .Net
    Replies:
    9
    Views:
    558
    Kishor
    Sep 27, 2003
  2. Jesper Sahner
    Replies:
    1
    Views:
    371
    Rhino
    Feb 1, 2005
  3. Nikhil

    Dynamic tracing of C code execution

    Nikhil, May 12, 2006, in forum: C Programming
    Replies:
    5
    Views:
    1,640
    Frank Pittel
    May 16, 2006
  4. Ggianks
    Replies:
    1
    Views:
    321
    Goran Pusic
    Aug 19, 2010
  5. James Britt
    Replies:
    0
    Views:
    103
    James Britt
    Oct 18, 2004
Loading...

Share This Page