Dynamic function execution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Wu

    Dynamic function execution

    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

  • Fredrik Lundh

    #2
    Re: Dynamic function execution

    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(**{"minute s": 30})

    </F>

    Comment

    • Irmen de Jong

      #3
      Re: Dynamic function execution

      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

      Comment

      • Cameron Laird

        #4
        Re: Dynamic function execution

        In article <mailman.718.11 64469686.32031. python-list@python.org >,
        Fredrik Lundh <fredrik@python ware.comwrote:
        >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(**{"minute s": 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(**{"minute s": 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(**{dimensi on: 30})

        ?

        Comment

        • John Machin

          #5
          Re: Dynamic function execution

          Cameron Laird wrote:
          In article <mailman.718.11 64469686.32031. python-list@python.org >,
          Fredrik Lundh <fredrik@python ware.comwrote:
          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(**{"minute s": 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(**{"minute s": 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(**{dimensi on: 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(**{argumen t_name: argument_value} )

          :-)

          Cheers,
          John

          Comment

          • Fredrik Lundh

            #6
            Re: Dynamic function execution

            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(**{dimensi on: 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>

            Comment

            Working...