Can anyone explain this?
Chad Netzer
cnetzer at mail.arc.nasa.gov
Tue Oct 22 16:24:58 EDT 2002
On Tuesday 22 October 2002 13:01, Henry Baumgartl wrote:
> Hello all,
>
> When i use the functions below without the print statement, Python responds
> with:
> def startTimer(self):
> self.startnow = time.time()
> def stopTimer(self):
> self.stopnow = time.time()
> wdur = self.stopnow - self.startnow
> wmin = int(wdur / 60)
> print str(wmin)
> self.wtime = self.wtime + wmin
> self.pcvar.set(str(self.wtime))
You will probably get a fair number of responses to this: But basically,
you are trying to use functions (callable objects that are NOT associated
with a Class or Instance), rather than member functions (callable objects
that ARE associated with a class.
By using "self", and self.startnow, etc. you are trying to save state in an
object, but that object doesn't exist. You could convert to using globals,
but that is lame. You need the state, so make this whole thing a class,
providing services in the form of member calls that preserve state. ie:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import time
class TimerThingy:
def __init__( self ):
# Initialize to help flag uses of stopTimer without startTimer
self.startnow = None
self.stopnow = None
return
def startTimer( self ):
self.startnow = time.time()
return
def stopTimer( self ):
self.stopnow = time.time()
def getDiff( self ):
return self.stopnow - self.startnow
if __name__ == '__main__':
t = TimerThingy()
t.startTimer()
print 'Sleeping for three seconds...'
time.sleep( 3 )
t.stopTimer()
wdur = t.getDiff()
wmin = int(wdur / 60)
print str(wmin)
# I don't know what pcval is, so I ignore all the rest
#wtime = wtime + wmin
#pcvar.set(str(wtime))
--
Chad Netzer
cnetzer at mail.arc.nasa.gov
More information about the Python-list
mailing list