Python代码:
class Importer:
from importlib import __import__, reload
from sys import modules
libname = ""
import_count = 0
module = None
def __init__(self, name):
self.libname = name
self.import_count = 0
def importm(self):
if self.libname not in self.modules:
self.module = __import__(self.libname)
else:
print("must reload")
self.module = self.reload(self.module)
self.import_count += 1
# test out Importer
importer = Importer("module")
importer.importm() # prints Hello
importer.importm() # prints Hello
importer.importm() # prints Hello (again)
print(importer.import_count)上面的Python (3.8.1)代码位于OnlineGDB,如果运行该代码,将给出一个错误:
TypeError: reload() takes 1 positional argument but 2 were given当我在Python中打开importlib库时,我看到了以下内容:
# ... (previous code; unnecessary)
_RELOADING = {}
def reload(module): ## this is where reload is defined (one argument)
"""Reload the module and return it.
The module must have been successfully imported before.
"""
if not module or not isinstance(module, types.ModuleType): ## check for type of module
raise TypeError("reload() argument must be a module")
try:
name = module.__spec__.name
except AttributeError:
name = module.__name__
if sys.modules.get(name) is not module: ## other code you (probably) don't have to care about
msg = "module {} not in sys.modules"
raise ImportError(msg.format(name), name=name)
if name in _RELOADING:
return _RELOADING[name]
_RELOADING[name] = module
try:
parent_name = name.rpartition('.')[0]
if parent_name:
try:
parent = sys.modules[parent_name]
except KeyError:
msg = "parent {!r} not in sys.modules"
raise ImportError(msg.format(parent_name),
name=parent_name) from None
else:
pkgpath = parent.__path__
else:
pkgpath = None
target = module
spec = module.__spec__ = _bootstrap._find_spec(name, pkgpath, target)
if spec is None:
raise ModuleNotFoundError(f"spec not found for the module {name!r}", name=name)
_bootstrap._exec(spec, module)
# The module may have replaced itself in sys.modules!
return sys.modules[name]
finally:
try:
del _RELOADING[name]
except KeyError:
pass
# ... (After code; unnecessary)所有双哈希标签(##)注释都是我的
很明显,reload确实有一个参数,它检查该参数是否是一个模块。在OGDB (OnineGDB)代码中,我只传递一个参数(非常肯定),并且它是类型模块(很可能)。如果我删除这个参数(您可以编辑OGDB),它会给出:
TypeError: reload() argument must be module因此,由于某种原因,Python一直认为我有一个比实际多一个论点。我让它工作的唯一方法是编辑importlib文件,使reload有两个参数(这不是一个好主意)。
我试过运行PDB,没什么用。
有人能发现任何明显的错误吗,比如实际上有两个争论?
发布于 2020-12-12 19:06:28
我需要做的是将import放在类之外,这样它才能正常工作。这是新OGDB。@L3viathan的学分。代码如下:
from importlib import __import__, reload
from sys import modules
class Importer:
libname = ""
import_count = 0
module = None
def __init__(self, name):
self.libname = name
self.import_count = 0
def importm(self):
if self.libname not in modules:
self.module = __import__(self.libname)
else:
print("must reload")
self.module = reload(self.module)
self.import_count += 1
# test out Importer
importer = Importer("module")
importer.importm() # prints Hello
importer.importm() # prints Hello
importer.importm() # prints Hello (again)
print(importer.import_count)发布于 2020-12-12 19:07:08
您有一个问题,因为您正在调用self.reload(self.module),这实际上相当于调用reload(self, self.module)。要查看这一点,请尝试运行以下命令:
class Example:
def some_method(*args):
print(args)
def some_other_method(self):
self.some_method(1)
an_example = Example()
example.some_other_method()您应该看到,尽管我们只将一个参数传递给self,而some_method没有self参数,但它输出了两个参数,而不是一个参数(第一个参数是对self的引用)。
最好在您的reload方法中导入importm方法(或者在类之外),如下所示:
def importm(self):
from importlib import __import__, reload
if self.libname not in self.modules:
self.module = __import__(self.libname)
else:
print("must reload")
self.module = reload(self.module)
self.import_count += 1发布于 2020-12-12 19:02:15
import mymodule
reload(mymodule)是如何运作的..。I‘我不知道你的问题是什么,从上面那堵大墙里看,这通常是用来将状态重置为初始状态的
mymodule.py
x = 5main.py
from importlib import reload # in py2 you did not need to import it
import mymodule
print(mymodule.x) # 5
mymodule.x = 8
print(mymodule.x) # 8
reload(mymodule)
print(mymodule.x) # 5 againhttps://stackoverflow.com/questions/65268686
复制相似问题