在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)]