小编Sam*_*Sam的帖子

Python:在无限循环函数内仅运行一次代码段..?

我有一个正在一遍又一遍地运行的函数。在该函数内部,我希望仅在该函数第一次运行时运行特定的段。

我不能使用函数外部的任何变量,例如

    firstTime = True

    myFunction(firstTime): #function is inside a loop
        if firstTime == True:
            #code I want to run only once
            firstTime = False
        #code I want to be run over and over again
Run Code Online (Sandbox Code Playgroud)

我也不想使用全局变量。

有什么想法如何实现这一点?

python loops function python-3.x

3
推荐指数
1
解决办法
8557
查看次数

标签 统计

function ×1

loops ×1

python ×1

python-3.x ×1