gpt4 book ai didi

python - 如何编写与使用它的类具有相同导入的装饰器?

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:59 25 4
gpt4 key购买 nike

我如何在 Python 中编写一个装饰器,它有一些导入 from Foo import Foo,当我想将它用于一个类的方法时,它是必需的,它也有这个导入?

将导入放入两个单独的文件中会导致错误,告诉我无法从 Foo 导入 Foo。

如果不可能,那么这个:如果装饰器不打算在其他任何地方使用,将装饰器放入与类相同的文件中,这样我只需要编写一次导入,就不会与任何其他导入冲突,这是个好主意吗?

在我的真实示例中,我想编写一个装饰器,它只是更改一个标志,该标志指示列表是否已更改。该类中修改列表的每个方法都将使用该装饰器进行装饰。

编辑#1:

更改代码结构后,我现在只有两个参与的源代码文件。这些是以下内容:

# -*- coding: utf-8 -*-
from AppSettings import AppSettings
from FileManager import FileManager
from decorators.changesvocables import changesvocables
from exceptions.DuplicateVocableException import DuplicateVocableException
from exceptions.UnknownVocableException import UnknownVocableException

__author__ = 'xiaolong'


class VocableManager:

vocables = []
search_result = []
vocables_changed = False

test_counter = 0
test_call_counter = 0

def __init__(self):
pass

@classmethod
@changesvocables
def remove_vocable(cls, vocable):
VocableManager.test_call_counter += 1
if vocable in VocableManager.vocables:
VocableManager.test_counter += 1
VocableManager.vocables.remove(vocable)
# del vocable
else:
print(vocable)
raise UnknownVocableException('Vocable not found.')
# VocableManager.vocables_changed = True

@classmethod
@changesvocables
def remove_vocable_by_index(cls, index):
del VocableManager.vocables[index]
# VocableManager.vocables_changed = True

@classmethod
@changesvocables
def add_vocable(cls, vocable):
if vocable not in VocableManager.vocables:
VocableManager.vocables.append(vocable)
else:
raise DuplicateVocableException('The vocable already exists.')
# VocableManager.vocables_changed = True

在那之后类仍然继续,但是没有其他带有 changesvocables 修饰的方法。如果需要,我可以添加整个类(class)。

装饰器:

# -*- coding: utf-8 -*-
from functools import wraps
from VocableManager import VocableManager as VocMan

__author__ = 'xiaolong'


def changesvocables(function):
@wraps(function)
def wrapper(*args, **kw):
result = function(*args, **kw)
VocMan.vocables_changed = True
return result
return wrapper

当我尝试运行应用程序时,这会导致以下错误:

** (__main__.py:16991): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-sQwlsgyRi2: Connection refused
Traceback (most recent call last):
File "/home/xiaolong/development/pycharm-workspace/gtkplus-tool/gtkplustool/__main__.py", line 1, in <module>
from Application import Application
File "/home/xiaolong/development/pycharm-workspace/gtkplus-tool/gtkplustool/Application.py", line 8, in <module>
from VocableManager import VocableManager
File "/home/xiaolong/development/pycharm-workspace/gtkplus-tool/gtkplustool/VocableManager.py", line 4, in <module>
from decorators.changesvocables import changesvocables
File "/home/xiaolong/development/pycharm-workspace/gtkplus-tool/gtkplustool/decorators/changesvocables.py", line 3, in <module>
from VocableManager import VocableManager as VocMan
ImportError: cannot import name 'VocableManager'

编辑#3:

这是一个 MCVE:

主要.py:

# -*- coding: utf-8 -*-
from mcve.ListManager import ListManager

__author__ = 'xiaolong'

if __name__ == '__main__':
list_manager = ListManager()
list_manager.add_item('item1')

装饰器:

# -*- coding: utf-8 -*-
from functools import wraps
from mcve.ListManager import ListManager as ListMan

__author__ = 'xiaolong'


def changesitems(function):
@wraps(function)
def wrapper(*args, **kw):
result = function(*args, **kw)
ListMan.vocables_changed = True
return result
return wrapper

列表管理器:

# -*- coding: utf-8 -*-
from mcve.changesitems import changesitems


class ListManager:

list_of_items = []

def __init__(self):
pass

@changesitems
def add_item(self, item):
if item not in ListManager.list_of_items:
ListManager.add_item(item)

和之前一样的错误:

Traceback (most recent call last):
File "/home/xiaolong/development/pycharm-workspace/MCVE/mcve/main.py", line 2, in <module>
from mcve.ListManager import ListManager
File "/home/xiaolong/development/pycharm-workspace/MCVE/mcve/ListManager.py", line 2, in <module>
from mcve.changesitems import changesitems
File "/home/xiaolong/development/pycharm-workspace/MCVE/mcve/changesitems.py", line 3, in <module>
from mcve.ListManager import ListManager as ListMan
ImportError: cannot import name 'ListManager'

Process finished with exit code 1

最佳答案

尝试:

from Foo import Foo as foo

现在您可以访问包,作为 Foo,也可以作为装饰器,作为 foo

关于python - 如何编写与使用它的类具有相同导入的装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com