[英]Python __init__ error if not there
如果没有init ,抛出错误的最pythonic方法是什么。
class test(object):
def __init__ (self,amount):
try:
self.orig_amount = amount
except TypeError:
raise TypeError("There must be an amount entered")
if __name__ == "__main__":
x = test()
我想这会抛出一个错误,说需要输入一个数量的测试()
也许这个:
class test(object):
def __init__ (self,amount = None): #Unless None is a valid amount, use any default value that is not permissible
if amount is None:
raise TypeError("There must be an amount entered")
self.orig_amount = amount
这样就可以获得自定义错误,而不是通用的“错误参数计数”错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.