在我的代码(Python)中,我有一个定义如下的类:
class Properties:
def __init__(self,**kwargs):
self.otherproperty = kwargs.get("otherproperty")
self.channel = kwargs.get("channel")
if self.channel not in "A":
print("bad channel input - it should be A")
pass
在代码的其余部分中,我在不同的位置计算各种属性,并将其添加到该类的实例中:
prop = Properties()
prop.channel="B"
#lots of calculations here
prop.otherproperty= "not a channel"
但是当我这样做时,我得到一个错误:TypeError: 'in <string>' requires string as left operand, not NoneType
我已经通过以下方式解决了问题,新属性得到了很好的验证,并且用户被告知prop.channel
应该是不同的:
prop = Properties(otherproperty = "not a channel",channel="B")
但由于几个原因,以这种方式定义prop
个对象将是不方便的.
那么,当我逐步添加对象的属性时,如何验证它们呢?这种循序渐进的物体建筑是如何专业命名的?