擅长:python、mysql、java
<p>类变量在模块加载时初始化,因此<code>foo.time</code>在h、m和s为零时设置。但是,如果将其作为类方法,则会得到正确的结果:</p>
<pre><code>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())
</code></pre>