Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

python:How to modify globals inside functions with schelude?

import time
import schedule

seconds = 0.01

def job():
    print("I'm working...")
    global seconds
    seconds += 0.01

schedule.every(seconds).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

It won't actually increase seconds every time it gets executed,sorry for the bad explanation was in a hurry

question from:https://stackoverflow.com/questions/65642914/pythonhow-to-modify-globals-inside-functions-with-schelude

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remove the line

seconds = 0.01

from your function. Every time this function is invoked, your variable seconds keeps getting re-initialised to 0.01, thus not letting it increase any further. You have already initialised it once at the time of declaration, so don’t need to do it again and again.

def job():
    print("I'm working...")
    global seconds
    seconds += 0.01

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...