Multiple modules , single instance of a class - Python -
Multiple modules , single instance of a class - Python -
i have 2 modules misc.py , main.py , define classes nowadays in misc.py in main.py.
below code
#misc.py class dummy: def __init__(self): pass def dummyprint(self): print "welcome python" #main.py import misc dummyobj = dummy() dummyobj.dummyprint() is right way go ? not see output i.e., welcome python
$python misc_main.py misc.py edit: added statement misc import dummy , getting next error
$python misc_main.py main.py traceback (most recent phone call last): file "misc_main.py", line 5, in <module> dummyobj = dummmy() nameerror: name 'dummmy' not defined
when next command, calling misc_main.py interpreter misc.py argument.
python misc_main.py misc.py since misc_main not reading command line arguments, equivalent
python misc_main.py i surprised not errors, in either case. need import actual class if want output.
from misc import dummy dummyobj = dummy() dummyobj.dummyprint() note, assuming main file in called misc_main.py rather main.py have stated in question. otherwise not invoking right file.
python
Comments
Post a Comment