堆叠 attrs 属性装饰器
stacking attrs attributes decorators
我正在尝试为两个属性重用验证器函数。我知道我可以在外部创建函数并在验证器中引用它,但是像这样堆叠装饰器在技术上是否正确:
In [6]: @attr.s
...: class A:
...: a = attr.ib()
...: b = attr.ib()
...:
...: @a.validator
...: @b.validator
...: def a_b_check(self, attribute, value):
...: assert value == 5
...:
In [7]: A(5,5)
Out[7]: A(a=5, b=5)
In [8]: A(5,6)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-8-ec475f254e70> in <module>
----> 1 A(5,6)
<attrs generated init 71300c05faf88cf49c2dcea04f8137146d4dac79> in __init__(self, a, b)
4 if _config._run_validators is True:
5 __attr_validator_a(self, __attr_a, self.a)
----> 6 __attr_validator_b(self, __attr_b, self.b)
<ipython-input-6-701125903dd2> in a_b_check(self, attribute, value)
7 @b.validator
8 def a_b_check(self, attribute, value):
----> 9 assert value == 5
10
AssertionError:
In [9]: A(6,5)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-9-4582d95e8377> in <module>
----> 1 A(6,5)
<attrs generated init 71300c05faf88cf49c2dcea04f8137146d4dac79> in __init__(self, a, b)
3 self.b = b
4 if _config._run_validators is True:
----> 5 __attr_validator_a(self, __attr_a, self.a)
6 __attr_validator_b(self, __attr_b, self.b)
<ipython-input-6-701125903dd2> in a_b_check(self, attribute, value)
7 @b.validator
8 def a_b_check(self, attribute, value):
----> 9 assert value == 5
10
AssertionError:
测试表明验证工作正常,但不确定是否发生了错误。
我希望它能正常工作,因为验证器的 @-notation doesn't change the method at all and just memorizes it.
如果您想重复使用验证器,我建议使用更通用的函数,并且也可以跨 class 工作。
我正在尝试为两个属性重用验证器函数。我知道我可以在外部创建函数并在验证器中引用它,但是像这样堆叠装饰器在技术上是否正确:
In [6]: @attr.s
...: class A:
...: a = attr.ib()
...: b = attr.ib()
...:
...: @a.validator
...: @b.validator
...: def a_b_check(self, attribute, value):
...: assert value == 5
...:
In [7]: A(5,5)
Out[7]: A(a=5, b=5)
In [8]: A(5,6)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-8-ec475f254e70> in <module>
----> 1 A(5,6)
<attrs generated init 71300c05faf88cf49c2dcea04f8137146d4dac79> in __init__(self, a, b)
4 if _config._run_validators is True:
5 __attr_validator_a(self, __attr_a, self.a)
----> 6 __attr_validator_b(self, __attr_b, self.b)
<ipython-input-6-701125903dd2> in a_b_check(self, attribute, value)
7 @b.validator
8 def a_b_check(self, attribute, value):
----> 9 assert value == 5
10
AssertionError:
In [9]: A(6,5)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-9-4582d95e8377> in <module>
----> 1 A(6,5)
<attrs generated init 71300c05faf88cf49c2dcea04f8137146d4dac79> in __init__(self, a, b)
3 self.b = b
4 if _config._run_validators is True:
----> 5 __attr_validator_a(self, __attr_a, self.a)
6 __attr_validator_b(self, __attr_b, self.b)
<ipython-input-6-701125903dd2> in a_b_check(self, attribute, value)
7 @b.validator
8 def a_b_check(self, attribute, value):
----> 9 assert value == 5
10
AssertionError:
测试表明验证工作正常,但不确定是否发生了错误。
我希望它能正常工作,因为验证器的 @-notation doesn't change the method at all and just memorizes it.
如果您想重复使用验证器,我建议使用更通用的函数,并且也可以跨 class 工作。