How do I abort object instance creation in Python?

前端 未结 2 2020
自闭症患者
自闭症患者 2021-02-05 03:30

I want to set up a class that will abort during instance creation based on the value of the the argument passed to the class. I\'ve tried a few things, one of them being raising

2条回答
  •  -上瘾入骨i
    2021-02-05 04:06

    Just raise an exception in the initializer:

    class a(object):
        def __init__(self, x):
            if not x:
                raise Exception()
    

提交回复
热议问题