Search code examples
pythonon-the-fly

How to change function parameters during runtime?


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


Solution

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