Local variable in each class - python timed loop -
i have python file in continuous loop reading external file, have lot of different info points i'm reading , have class (fb) i'm calling feed locations of each point (m1.x, m2.x etc....) file loops every 30 seconds.
i need variable in loop not reset each instance i'm using. if define within loop it's reset , if utilize global variable can't utilize multiple variables.
so in illustration below 'test' counts 1 instance global variable , self.x reset after each loop. neither of need. attempting utilize threading causes more problems modules i'm using. if has ideas on how create local variable not reset within class looping great. thanks.
test = 0 s = sched.scheduler(time.time, time.sleep) def loop1(sc): class fb: def link(self): global test test = test + 1 print test self.x = self.x + 1 print self.x m1 = fb() m1.x = 1 m1.link() m2 =fb() m2.x = 0 m2.link() # update loop every 10 sec sc.enter(10, 10, loop1, (sc,)) # end loop s.enter(1, 1, loop1, (s,)) s.run()
i think want object y counts turns of loop, not number of instances created in each turn (= self.x), nor cumulative number of instances created since start of programme (= test)
i've got next programme obtain object y increments 1 @ each turn of loop. needs creates exclusively new class @ origin of turn. instruction
fb = type('fb',(object,),{'__init__':initer, 'link':linker, 'classies':0})
does creation of new class. evidenced display of classes created: print 'id(fb) == %d' % id(fb)
identity of object place lies in memory. if not same id, not same object:
import sched,time test = 0 s = sched.scheduler(time.time, time.sleep) y = 2000 def initer(self): global test,y if self.__class__.classies == 0: y += 1 self.__class__.classies += 1 def linker(self): global test,y test = test + 1 self.x = self.x + 1 print 'y == %d' % y print 'test == %d self.x == %d' % (test,self.x) def loop1(sc): fb = type('fb',(object,),{'__init__':initer, 'link':linker, 'classies':0}) print '--------' print 'id(fb) == %d' % id(fb) m1 = fb() m1.x = 0 m1.link() print m2 =fb() m2.x = 1 m2.link() print m3 =fb() m3.x = 2 m3.link() print '--------' # update loop every 10 sec sc.enter(10, 10, loop1, (sc,)) # end loop s.enter(1, 1, loop1, (s,)) s.run()
displays
-------- id(fb) == 18976648 y == 2001 test == 1 self.x == 1 y == 2001 test == 2 self.x == 2 y == 2001 test == 3 self.x == 3 -------- -------- id(fb) == 13818640 y == 2002 test == 4 self.x == 1 y == 2002 test == 5 self.x == 2 y == 2002 test == 6 self.x == 3 -------- -------- id(fb) == 18970384 y == 2003 test == 7 self.x == 1 y == 2003 test == 8 self.x == 2 y == 2003 test == 9 self.x == 3 -------- -------- id(fb) == 18970864 y == 2004 test == 10 self.x == 1 y == 2004 test == 11 self.x == 2 y == 2004 test == 12 self.x == 3 -------- -------- id(fb) == 18971736 y == 2005 test == 13 self.x == 1 y == 2005 test == 14 self.x == 2 y == 2005 test == 15 self.x == 3 -------- -------- id(fb) == 18957224 y == 2006 test == 16 self.x == 1 y == 2006 test == 17 self.x == 2 y == 2006 test == 18 self.x == 3 -------- . . . etc
i obtain result above way. deleting fb
@ end of each turn isn't sufficient, see if create next code run. code few more instructions, have y
, display of id(fb)
. you'll see `` an
id(fb)`` remains same 1 turn other.
import sched,time test = 0 s = sched.scheduler(time.time, time.sleep) y = 2000 def loop1(sc): class fb: def link(self): global test,y test = test + 1 self.x = self.x + 1 print 'y == %d' % y print 'test == %d self.x == %d' % (test,self.x) print 'id(fb) == %d' % id(fb) print '--------' m1 = fb() m1.x = 0 m1.link() print m2 =fb() m2.x = 1 m2.link() print m3 =fb() m3.x = 0 m3.link() print '--------' del fb # update loop every 10 sec sc.enter(10, 10, loop1, (sc,)) # end loop s.enter(1, 1, loop1, (s,)) s.run()
the reason why code doesn't work like, far have understand want, portion of script defines class called "definition of class" (incredible, isn't ?) , when interpretr passes on class block first time, executes it. execution of "definition of class" creates object class. class created, "definition" not re-executed if interpreter passes 1 time again on class' code block (= class definition). here 1 time again , there ambiguous word: 'definition' can understood 'the text block in script defines class' or 'the process of executing instructions define creation of class , lead object beingness class' sense used "definition of class" 1 used in doc:
class definitions, function definitions (def statements) must executed before have effect. (you conceivably place class definition in branch of if statement, or within function.)
http://docs.python.org/2/tutorial/classes.html#class-definition-syntax
so, finally, want in code, "definition of class fb" executed 1 time, it's same class modeling classes , turn after turn of loop.
python
No comments:
Post a Comment