В php, как и в JavaScript можно делать счетчики с помощью замыкания.
Пример кода на php:
function counter() {
$counter = 0;
return function() use (&$counter) {
$counter++;
return $counter;
};
}
$a = counter();
var_dump($a()); // 1
var_dump($a()); // 2
Возможно ли сделать подобное в Python?
Такой код дает ошибку: UnboundLocalError: local variable 'counter' referenced before assignment
def counter():
counter = 0
def increase():
counter = counter+1
return counter
return increase
a = counter()
print(a())