-1

i have to check if the content of some variable are of some specific type (it is a sanity check i want to implement). If each one of these is not of the specified type then the program has to abort.

I'm not experienced in python, i've a glance to the official guide on the web where it is stated that it is possible to define own exceptions by class implementation. I don't know if that would be hard or not, but since i don't have to do something particularly complicated is it possible to do that in a simple way?

Update

I've implemented the exception int exception in the following way:

class TypeCheckingException(Exception):
    def __init__(self,type_in,type_exp):
        self.type_in = type_in
        self.type_exp = type_exp

The specific line of the implementation its use are:

try:
        if isinstance(var1,str) == 0:
            raise TypeCheckingException(type(var1),str)
        if isinstance(var2,str) == 0:
            raise TypeCheckingException(type(var2),str)
        if isinstance(var3,int) == 0:
            raise TypeCheckingException(type(var3),int)
        if isinstance(var4,int) == 0:
            raise TypeCheckingException(type(var4),int)
        if isinstance(var5,str) == 0:
            raise TypeCheckingException(type(var5),str)
        if isinstance(var6,float) == 0:
            raise TypeCheckingException(type(var6),float)
    except TypeCheckingException, tce:
        print "Type expected input " + str(tce.type_in) + " while type expected is " + str(tce.type_exp) +  "."

In this case works, but the error output is quite verbose... i'd like to make it shorter...

Any clue?

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • `class MyOwnException(Exception): pass` – there, this is an implementation of a custom exception. You can now `raise` that. Is that what you are confused about? What exactly are you having a problem with? – deceze Sep 29 '15 at 13:20
  • possible duplicate of [Differences between isinstance() and type() in python](http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python) – Morgan Thrapp Sep 29 '15 at 13:22
  • The problem is that i have a set of variable, derived by internal computation (so i don't pass them as argument) since the program is quite large i would like to check them at the end of the computation to make sure they're of the exptected type. If such variable aren't of the expected type the program has to abort (possibly with an exception which points what is the problem). – user8469759 Sep 29 '15 at 13:25
  • Sooooo.... `if not isinstance(foo, bar): raise MyOwnException()`...!? Again, what's your actual hangup? – deceze Sep 29 '15 at 13:29
  • I need an explicit example of the implementation of such exception, please. – user8469759 Sep 29 '15 at 13:29
  • Ah ok, i thought it was more C++ style, let me try. – user8469759 Sep 29 '15 at 13:30
  • possible duplicate of [Python - Implementing Custom exceptions](http://stackoverflow.com/questions/32005875/python-implementing-custom-exceptions) – deceze Sep 29 '15 at 13:30

1 Answers1

1

You want to use isinstance(variable, type).

>>> a = 'str'
>>> isinstance(a, str)
True

You won't need to use exception handling with this route. You can check for instance type, then exit, if necessary.

Programmer
  • 293
  • 4
  • 15