如何在运行时更改函数参数?

发布于 2024-11-26 18:29:00 字数 307 浏览 3 评论 0原文

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

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

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

I'm a beginner and use Python 2.7. I want to make the definitions parameters to be changeable so I can controll the pause and string output on the fly. Is this possible? I've read some thread stuff but it seems to be more about executing two tasks at the same time. I want communication between the two tasks during runtime.

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

Any solution or link to documentation is very much appreciated. Thanks for your time!
/Karl

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听闻余生 2024-12-03 18:29:01

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

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

在调用此函数的某个地方:

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

Threads are for simultaneous work. I guess if you just redesign your code you will have the effect you want. Consider removing the while clause from you function and put it outside:

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

And somewhere you call this function:

while True:
    stringToWrite, pauseSeconds = gatherSomeInformation()
    writeAndPause(stringToWrite, pauseSeconds)
笑脸一如从前 2024-12-03 18:29:01

@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所说的去做。

@Constantinius is right: the answer is almost certainly to redesign your code so you don't need to do something esoteric.

I'll describe another way to do it purely for fun. If you really wanted to keep that while loop inside the function, you can do so with a Yield Expression

For example:

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

This can be used in the following way:

# 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'

Now that you've seen it, forget it and do what @Constantinius said.

别忘他 2024-12-03 18:29:01

这对你有帮助吗?

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

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

Does this help you ?

It takes advantage of the characteristics of default argument definition and the fact a list isn't a variable but a collection of reference, in my code only one (shortly said)

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()

result

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文