在Python中,我们有一个叫做
澄清:下面在C++中使用的
所以,Svar正在保持它的价值。
nonlocal
的关键字。它和C++中的static
一样吗?如果在Python中有嵌套函数,我们可以在外部函数中声明变量而不是在内部函数中使用nonlocal
,这样它就真正成为了nonlocal
。澄清:下面在C++中使用的
static
关键字:
#include <iostream>
int foo () {
static int sVar = 5;
sVar++;
return sVar;
}
using namespace std;
int main () {
int iter = 0;
do {
cout << "Svar :" foo() << endl;
iter++;
} while (iter < 3);
}
在迭代过程中输出结果:
Svar :6
Svar :7
Svar :8
所以,Svar正在保持它的价值。
static
是一个关键字,其含义取决于上下文的不同。 - 463035818_is_not_a_number