Acceder a la variable de una función desde otra función
from threading import Thread
import time
def print_k():
while true:
if main.k % 2 == 1: # ditto
print(main.k, "is even.") # <-- my problem is HERE ( Ignore all the other stuff )
time.sleep(2)
def main():
k = 1
while k != 200:
k += 1
print k
time.sleep(0.5)
if __name__ == '__main__':
Thread(target=print_k).start()
Thread(target=main).start()
en este script (solo ejemplo, ignore todas las funcionalidades realistas) Estoy tratando de ejecutarmain()
, que suma hasta 200 y lo imprime, y enprint_k
estoy imprimiendomain
variable de k. Tengo una excepción planteada, como era de esperar, y me pregunto cómo puedo acceder a la variable de una función separada desde una función diferente (por cierto, ambas se ejecutan al mismo tiempo, de ahí el módulo Threading).