Is it possible to make a class call del()
for some instances under some condition? Or turn self into None?
class T:
def __init__(self, arg1, arg2):
condition=check_condition(arg1,arg2)
if not condition:
do_something(arg1)
else:
del(self) #or self=None or return None
I need to do something like this to be sure that will never exist a kind of instance.
To disable deletion protection, set deletionProtection to false . // and waits for it to complete.
gcloud compute instances list lists summary information for the virtual machine instances in a project. The “--uri” option can be used to display the URIs of the instances' in the project. Users who want to see more data should use gcloud compute instances describe . By default, instances from all zones are listed.
I need do something like this to be sure that will never exist a kind of instance.
If you simply want to prevent the creation of such an instance, raise an exception in __init__()
whenever the condition is satisfied.
This is standard protocol for signalling constructor failures. For further discussion, see Python: is it bad form to raise exceptions within __init__?
Look into __new__
. You should be able to detect the condition you care about and return None
. As @lzkata mentions in the comments, raising an Exception
is probably a better approach, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With