循环相关类属性与代码局部性

2024-04-26 12:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当您有两个类需要具有相互引用的属性时

# DOESN'T WORK
class A:
    b = B()

class B:
    a = A()
# -> ERROR: B is not defined

standardanswers假设使用python是动态的这一事实,即

class A:
    pass

class B:
    a = A()

A.b = B()

从技术上解决了问题。但是,当有三个或更多的共同依赖类时,或者当这些类的长度超过几行时,这种方法会产生极难导航的意大利面代码。例如,我发现自己编写的代码如下:

class A:
    <50 lines>
    # a = B() but its set later
    <200 more lines>

class B:
    <50 lines>
    a = A()
    <100 lines>

A.b = B()  # to allow for circular referencing

这最终违反了DRY(因为我在两个地方编写代码)和/或将相关代码移到模块的另一端,因为我不能将A.b = B()放在它相关的类中。你知道吗

有没有更好的方法来允许python中循环依赖的类属性,而不涉及将相关代码分散到模块中通常较远的部分?你知道吗


Tags: 模块方法代码属性isnot动态error
1条回答
网友
1楼 · 发布于 2024-04-26 12:25:17

经过一段时间的试验,我找到了一种方法(主要是)做我想做的事。你知道吗

class DeferredAttribute:
    """ A single attribute that has had its resolution deferred """
    def __init__(self, fn):
        """fn - when this attribute is resolved, it will be set to fn() """
        self.fn = fn

    def __set_name__(self, owner, name):
        DeferredAttribute.DEFERRED_ATTRIBUTES.add((owner, name, self))

    @classmethod
    def resolve_all(cls):
        """ Resolves all deferred attributes """
        for owner, name, da in cls.DEFERRED_ATTRIBUTES:
            setattr(owner, name, da.fn())
        cls.DEFERRED_ATTRIBUTES.clear()

使用这个的习惯用法是

class A:
    @DeferredAttribute
    def b():
        return B()

class B:
    a = A()

DeferredAttribute.resolve_all()

这将生成类AB,它们与运行代码时完全相同

class A:
    pass

class B:
    a = A()

A.b = B()

结论:这有助于代码组织,避免重复和本地化相关代码。你知道吗

缺点是,它会打乱动态编程的一些预期;在调用resolve_deferred_attributes之前,值A.b将是一个特殊值,而不是B的实例。似乎可以通过向DeferredAttribute添加适当的方法来部分地解决这个问题,但我看不到一个完美的方法。你知道吗

编者注:上面的代码让我的IDE(PyCharm)对我大喊大叫,说def b():应该接受一个参数(尽管它运行良好)。如果需要,可以通过更改代码将错误更改为警告:

In the resolve_all method, change:
    setattr(owner, name, da.fn())

    ->

    fn = da.fn
    if isinstance(fn, staticmethod):
        setattr(owner, name, fn.__func__())
    else:
        setattr(owner, name, fn())

And in the use code, change:
    @defer_attribute
    def b():
        ...

    -> 

    @defer_attribute
    @staticmethod
    def b():
        ...

除了关闭警告之外,我还没有找到完全消除警告的方法。你知道吗

相关问题 更多 >