Incidentally, I have a question for anyone familiar with Python.
Is there a way to tell the interpreter that certain commands MUST be executed in order?
e.g.
Quote:
class MyClass():
....def __init__(self, var1):
....self.var1=var1
....def Method1(self):
........subprocess.call("os call")
........return
def main():
....ClassList=[]
....for i in range(5):
........ ClassList.append(MyClass(i))
....for j in ClassList:
........j.Method1()
........subprocess.call("os call")
........ other work
main()
|
When I run this and it gets to that 2nd loop instead of executing as:
ClassList[0].Method1()
subprocess.call
other work
ClassList[1].Method1()
subprocess.call
other work
ClassList[2].Method1(0
....
I get:
ClassList[0].Method1()
ClassList[1].Method1()
ClassList[2].Method1()
ClassList[3].Method1()
ClassList[4].Method1()
subprocess.call
other work
subprocess.call
other work
subprocess.call
other work
subprocess.call
other work
subprocess.call
other work
Clearly what's happening is that the interpreter looks at Method1, decides that it's not doing anything that is internally dependent on order of execution, so it optimizes by lumping all 5 executions together. The problem is, the results of the subprocess call ARE dependent on order of execution.
I came up with a workaround. I threw a dummy variable assignment (garbage=self.var1) at the end of Method1. That's enough to convince the interpreter that work is being done and it should keep things in order. But that seems inelegant. Is there a better way to stop the interpreter from thinking it knows better than me?