在Python Console中执行,会报错,错误提示与代码匹配不上。例如:
runfile('D:/project/python/example/pythonExample/LanguageExample/ObjectOrient/biz_handle.py', wdir='D:/project/python/example/pythonExample/LanguageExample/ObjectOrient')
gen_report:Script 李涛
Sname=Script,name=first
Traceback (most recent call last):
File "D:ProgramDataAnaconda3libsite-packagesIPythoncoreinteractiveshell.py", line 3325, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-49-9b4d67365a2c>", line 1, in <module>
runfile('D:/project/python/example/pythonExample/LanguageExample/ObjectOrient/biz_handle.py', wdir='D:/project/python/example/pythonExample/LanguageExample/ObjectOrient')
File "C:Program FilesJetBrainsPyCharm 2019.2helperspydev_pydev_bundlepydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:Program FilesJetBrainsPyCharm 2019.2helperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "D:/project/python/example/pythonExample/LanguageExample/ObjectOrient/biz_handle.py", line 23, in <module>
ss = Special("Special")
TypeError: Special() takes no arguments
在Terminal执行,就正常运行:
(venv) D:projectpythonexamplepythonExampleLanguageExampleObjectOrient>python biz_handle.py
gen_report:Script 李涛
Sname=Script,name=first
gen_report:韩梅梅
Sname=Special,name=Special
(venv) D:projectpythonexamplepythonExampleLanguageExampleObjectOrient>
在网上查了一下,Python Console是Python的交互式界面,Terminal是命令行模式。 但并没有找到资料解释他们之间的详细差别,或许大家都认为他们的效果是一样的,也没有人遇到过我这种情况。
相关源码如下:
创建Python文件 biz_handle.py
# encoding:utf-8
from general_logic import GeneralLogic
class Script(GeneralLogic):
def __init__(self, name):
self.Sname = self.__class__.__name__
self.name = name
def gen_report(log):
print("gen_report:{}".format(log))
class Special(GeneralLogic):
pass
if __name__ == "__main__":
so = Script("first")
so.check_run(gen_report,"Script 李涛")
ss = Special("Special")
ss.check_run(gen_report,"韩梅梅")
创建Python文件 general_logic.py
# encoding:utf-8
class GeneralLogic(object):
def __init__(self, name):
self.Sname = self.__class__.__name__
self.name = name
def check_run(self,gen_report,log):
gen_report(log)
print("Sname={},name={}".format(self.Sname,self.name))
谁知道该如何排查他们之间的差别?
版本信息:
(venv) D:projectpythonexamplepythonExampleLanguageExampleObjectOrient>python -V
Python 3.7.3
print(sys.version)
3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]