Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python class attribute not updating when updated in a function

Tags:

python

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.

like image 986
Shirb Avatar asked Feb 19 '21 23:02

Shirb


2 Answers

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())
like image 143
rhurwitz Avatar answered Sep 20 '22 11:09

rhurwitz


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.

like image 34
GAP2002 Avatar answered Sep 16 '22 11:09

GAP2002