module - Python class can't be updated after being compiled -
i started python couple of days ago, coming c++ background. when write class, call script, , afterwards update interface of class, behaviour find unintuitive.
once compiled, class seems not changeable anymore. here example:
testmodule.py:
class testclass: def __init__(self,_a): self.first=_a def method(self, x, y): print x
testscript.py:
import testmodule tm=testmoduleb.testclass(10) tm.method(3, 4)
execution gives me
3
now change argument list of method
:
def method(self, x):
, delete testmodule.pyc , in script call
tm.method(3)
as result, get
typeerror: method() takes 3 arguments (2 given)
what doing wrong? why script not use updated version of class? use canopy editor saw behaviour python.exe interpreter.
and apologies, if similar asked before. did not find question related one.
python loads code objects memory; class
statement executed when file first imported class object created , stored in module namespace. subsequent imports re-use created objects.
the .pyc
file used next time module imported for first time python session. replacing file not result in module reload.
you can use reload()
function force python replace already-loaded module fresh code disk. note , other direct references class not replaced; instance of testclass
class (tm
in case) still reference old class object.
when developing code, easier restart python interpreter , start afresh. way don't have worry hunting down direct references , replacing those, example.
Comments
Post a Comment