【问题标题】:How to change function parameters during runtime?如何在运行时更改函数参数?
【发布时间】:2011-07-26 13:06:45
【问题描述】:

我是初学者,使用 Python 2.7。我想让定义参数可变,这样我就可以动态控制暂停和字符串输出。这可能吗?我读过一些线程的东西,但似乎更多的是同时执行两个任务。我希望在运行时两个任务之间进行通信。

    def writeAndPause(stringToWrite,pauseSeconds)
        while True:
            print stringToWrite
            sleep(pauseSeconds)

非常感谢任何解决方案或文档链接。谢谢你的时间! /卡尔

【问题讨论】:

    标签: python on-the-fly


    【解决方案1】:

    线程用于同时工作。我想如果你只是重新设计你的代码,你就会得到你想要的效果。考虑从您的函数中删除 while 子句并将其放在外面:

    def writeAndPause(stringToWrite,pauseSeconds)
        print stringToWrite
        sleep(pauseSeconds)
    

    你在某个地方调用这个函数:

    while True:
        stringToWrite, pauseSeconds = gatherSomeInformation()
        writeAndPause(stringToWrite, pauseSeconds)
    

    【讨论】:

      【解决方案2】:

      @Constantinius 是对的:答案几乎可以肯定是重新设计你的代码,这样你就不需要做一些深奥的事情了。

      我将描述另一种纯粹为了好玩的方式。如果您真的想将 while 循环保留在函数中,可以使用 Yield Expression

      例如:

      def writeAndPause():
          while True:
              stringToWrite, pauseSeconds = yield
              print stringToWrite
              sleep(pauseSeconds)
      

      这可以通过以下方式使用:

      # create the generator
      writer = writeAndPause()
      # start the generator
      writer.next()
      # resume execution and send new values into generator
      writer.send(('start string', 10))
      'start string'
      # resume execution and send another set of new values into generator
      writer.send(('new string', 20))
      'new string'
      

      既然你已经看到了,那就忘掉它,照@Constantinius 所说的去做吧。

      【讨论】:

        【解决方案3】:

        这对你有帮助吗?

        它利用了默认参数定义的特性以及列表不是变量而是引用集合这一事实,在我的代码中只有一个(简短地说)

        from time import sleep,time
        
        stringToWrite = [None]
        pauseSeconds = [0]
        
        def writeAndPause(stw = stringToWrite, pz = pauseSeconds, keep = [time()]):
                if stw[0]:
                    print stw[0]
                else:
                    print 'START'
                print '  having waited ',time()-keep[0],'seconds,    must wait',pz[0],'seconds'
                keep[0] = time()
                sleep(pz[0])
        
        
        writeAndPause()
        
        
        for a,b in (('first',1),('second',2.05),('third',3.4),('fourth',0.88),
                    ('BANANA',0.2),('APPLE',1.5),
                    ('PEAR',0.77),('CHERRY',4),
                    ('ORANGE',0.1),('NUT',6),
                    ('APRICOT',0.56),('PLUM',2.5)):
        
            stringToWrite[0] = a
            pauseSeconds[0] = b
            writeAndPause()
        

        结果

        START
          having waited  0.0310001373291 seconds,    must wait 0 seconds
        first
          having waited  0.0320000648499 seconds,    must wait 1 seconds
        second
          having waited  1.01600003242 seconds,    must wait 2.05 seconds
        third
          having waited  2.15600013733 seconds,    must wait 3.4 seconds
        fourth
          having waited  3.42100000381 seconds,    must wait 0.88 seconds
        BANANA
          having waited  0.905999898911 seconds,    must wait 0.2 seconds
        APPLE
          having waited  0.266000032425 seconds,    must wait 1.5 seconds
        PEAR
          having waited  1.51499986649 seconds,    must wait 0.77 seconds
        CHERRY
          having waited  0.796999931335 seconds,    must wait 4 seconds
        ORANGE
          having waited  4.03200006485 seconds,    must wait 0.1 seconds
        NUT
          having waited  0.140000104904 seconds,    must wait 6 seconds
        APRICOT
          having waited  6.03099989891 seconds,    must wait 0.56 seconds
        PLUM
          having waited  0.765000104904 seconds,    must wait 2.5 seconds
        

        【讨论】:

        • 虽然对于参数列表可用的情况来说,这是一个很好的解决方案,但进行一段时间构造非常重要
        • @user863479 我的回答的核心是writeAndPause()的定义,借助Python的特性做出你想要的效果,希望是你想要的效果。包含在 for 循环中的其余部分只是显示功能的示例,但您必须为此部分编写自己的代码。如果你想要一段时间,你在你的算法中使用一段时间,我不知道,我不能在不知道的情况下将整个代码应用于你的问题。事实上,我的回答是君士坦丁尼乌斯回答中表达的想法的具体化。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-02
        • 2011-06-20
        相关资源
        最近更新 更多