10

I have a stopwatch of sorts going in the background using threading, and while it's updating the global variable, it doesn't change the output of my class attribute.

This is what I have:

import time
from threading import Thread
s = 0
m = 0
h = 0
stopped = False

def stopwatch():
    global s
    global m
    global h
    global stopped
    while stopped == False:
        s = s + 1
        if s >= 60:
            s = 0
            m += 1
        if m >= 60:
            m = 0
            h += 1
        time.sleep(1)

class foo:
    name = 'shirb'
    time = str(h) + 'h' + str(m) + 'm' + str(s) +'s'

Thread(target = stopwatch).start()
input('press enter to stop the stopwatch')
stopped = True
print('Name: ' + foo.name + '\nTime: ' + foo.time)

Lets say I wait for one minute and 34 seconds. The output should be:

press enter to stop the stopwatch
Name: shirb
Time: 0h1m34s

But this is what it actually puts out:

press enter to stop the stopwatch
Name: shirb
Time: 0h0m0s

I have no idea what is causing it to not update. When I try to print the variable itself with "print(s)" i get the correct amount of seconds, so there is something wrong with the class attribute that I don't know how to fix.

2 Answers 2

11

Class variables are initialized at module load time, so foo.time is set when h, m, and s, are zero. If you make it a class method, however, you will get the right result:

class foo:
    name = 'shirb'
    
    @classmethod
    def cls_time(cls):
        return str(h) + 'h' + str(m) + 'm' + str(s) +'s'

Thread(target = stopwatch).start()
input('press enter to stop the stopwatch')
stopped = True
print('Name: ' + foo.name + '\nTime: ' + foo.cls_time())
Sign up to request clarification or add additional context in comments.

Comments

4

You could perhaps use just the one class:

import time
from threading import Thread


class stopwatch:
    def __init__(self):
        self.s = 0
        self.m = 0
        self.h = 0
        self.stopped = False
        self.name = "shirb"

    def begin(self):
        while self.stopped is False:
            self.s += 1
            if self.s >= 60:
                self.s = 0
                self.m += 1
            if self.m >= 60:
                self.m = 0
                self.h += 1
            time.sleep(1)

    def get_time(self):
        return str(self.h) + "h" + str(self.m) + "m" + str(self.s) + "s"


s = stopwatch()
Thread(target=s.begin).start()
input("press enter to stop the stopwatch")
s.stopped = True
print("Name: " + s.name + "\nTime: " + s.get_time())

This solves the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.