11

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.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Arthur Julião
  • 849
  • 1
  • 14
  • 29
  • 8
    ... `raise Exception()` ? – Izkata Nov 06 '12 at 21:48
  • 1
    [This question](http://stackoverflow.com/questions/7989042/preventing-a-class-from-direct-instantiation-in-python) has a bunch of answers that can help – David Pärsson Nov 06 '12 at 21:51
  • Good idea Izkata. My problem was: the class T is a value-object that contains their own validation method. In not-valid cases, the object should not be initialized (and destroyed, if possible). – Arthur Julião Nov 06 '12 at 21:55

5 Answers5

8

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__?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
2

You can raise an exception as suggested in comments. You can implement __new__. Also you can make some factory class, like

class TFactory:
    def createT(self, arg1, arg2):
        condition=check_condition(arg1,arg2)
        if not condition:
           return do_something(arg1)
        else:
           return None     
Vic
  • 21,473
  • 11
  • 76
  • 97
2

Yes, you can call del self. it works just fine, but you should know that all it's doing is deleting the reference to an instance, named "self" in the init method.

In python, objects exist as long as there is a reference to them somewhere.

If you want the object to never exist under certain conditions, then do not ever attempt to create an instance under those conditions, outside of init where you are creating the object.

Also, you could define a helpful function in this class to return whether those conditions are satisfied.

user1552512
  • 869
  • 1
  • 9
  • 14
1

The class constructor is intended to really construct an instance of its class and not fail half-silently by just not constructing an instance and returning None. Nobody would expect that behaviour.

Rather use a factory function.

class Test(object):
    @classmethod
    def create(cls):
        if ok(): return cls()
        else: return None
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • 1
    I agree that returning None is never a good idea -- especially since `__init__()` doesn't have a return value anyway. ;-) – martineau Nov 06 '12 at 22:22