-1

I use the code:

def foo(x): print(type(x)) if x is not int: raise(TypeError) #do smth

foo(100)

The log is , but TypeError is raised. Why?

3 Answers3

0

You need to use if type(x) is not int:

def foo(x):
    print(type(x))
    if type(x) is not int:
        raise(TypeError)
Swan Toma
  • 136
  • 6
0

So if im reading it right it should be:

def foo(x): if type(x) is not int: raise(TypeError) foo(100)

Im really not sure what you mean with log but maybe this helps

bot_diyar
  • 80
  • 7
0

It's better to use isinstance() rather than type() ==. The former works for subclasses.

rpoleski
  • 988
  • 5
  • 12