首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-2参数而不是一个参数?

Python-2参数而不是一个参数?
EN

Stack Overflow用户
提问于 2020-12-12 18:58:08
回答 3查看 82关注 0票数 0

Python代码:

代码语言:javascript
复制
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,如果运行该代码,将给出一个错误:

代码语言:javascript
复制
TypeError: reload() takes 1 positional argument but 2 were given

当我在Python中打开importlib库时,我看到了以下内容:

代码语言:javascript
复制
# ... (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),它会给出:

代码语言:javascript
复制
TypeError: reload() argument must be module

因此,由于某种原因,Python一直认为我有一个比实际多一个论点。我让它工作的唯一方法是编辑importlib文件,使reload有两个参数(这不是一个好主意)。

我试过运行PDB,没什么用。

有人能发现任何明显的错误吗,比如实际上有两个争论?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-12 19:06:28

我需要做的是将import放在类之外,这样它才能正常工作。这是新OGDB@L3viathan的学分。代码如下:

代码语言:javascript
复制
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)
票数 0
EN

Stack Overflow用户

发布于 2020-12-12 19:07:08

您有一个问题,因为您正在调用self.reload(self.module),这实际上相当于调用reload(self, self.module)。要查看这一点,请尝试运行以下命令:

代码语言:javascript
复制
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方法(或者在类之外),如下所示:

代码语言:javascript
复制
    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
票数 0
EN

Stack Overflow用户

发布于 2020-12-12 19:02:15

代码语言:javascript
复制
import mymodule

reload(mymodule)

是如何运作的..。I‘我不知道你的问题是什么,从上面那堵大墙里看,这通常是用来将状态重置为初始状态的

mymodule.py

代码语言:javascript
复制
x = 5

main.py

代码语言:javascript
复制
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 again
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65268686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档