【发布时间】:2021-04-13 10:50:52
【问题描述】:
我正在用 Python 编写具有以下架构的代码。基本上,它是一个模块化合成器,由“模块”元组组成。现在为简单起见,每个模块都是原子的(没有嵌套模块)。
每个模块包含两个参数,p1 和 p2。我想确保同一合成器中的每个模块对于 p1 和 p2 具有相同的参数值。
这是一种方法,它有一些样板:
DEFAULT_P1 = ...
DEFAULT_P2 = ...
class Module:
"""
Abstract base class.
"""
def __init__(
self, p1: int = DEFAULT_P1, p2: int = DEFAULT_P2
):
self.p1 = p1
self.p2 = p2
class ModuleK(Module):
"""
A handful of submodules.
"""
def __init__(
self,
... other params
p1: int = DEFAULT_P1,
p2: int = DEFAULT_P2,
):
super().__init__(p1=p1, p2=p2)
...
class Synth:
"""
An abstract class for a modular synth, ensuring that all modules
have the same sample and control rate.
"""
def __init__(self, modules: Tuple[Module]):
# Check that we are not mixing different control rates or sample rates
for m in modules[:1]:
assert m.p1 == modules[0].p1
assert m.p1 == modules[0].p2
这是使用全局变量的更简洁的方法。我担心这会产生副作用,即不能在同一运行时拥有两个具有不同 p1 和 p2 的合成器,除非你做一些非常繁琐和脆弱的事情。
DEFAULT_P1 = ...
DEFAULT_P2 = ...
class Module:
"""
Abstract base class.
"""
def __init__(
self
):
self.p1 = DEFAULT_P1
self.p2 = DEFAULT_P2
class ModuleK(Module):
"""
A handful of submodules.
"""
def __init__(
self,
... other params
):
...
我也考虑过从一个封装了 p1 和 p2 的类继承。但是,您仍然需要检查同一个合成器中的所有模块是否都继承自同一种封装类。由于它只有两个参数,因此不会以任何方式使事情更符合人体工程学。
有没有我遗漏的模式?
【问题讨论】:
-
不是每个模块都有自己的采样率和控制率,也许一个模块可以依赖于某种
SynthContext类提供的这些值?似乎永远不会有这样的情况,您可以拥有一个模块而不将它也附加到合成器。 -
FWIW,我认为您的第一种方法很好。也就是说,我认为您希望在其中使用
for m in modules[1:]:进行检查。
标签: python inheritance design-patterns