David Williams
David Williams

Reputation: 8654

Python type() function issue, str not callable

I am working on validating a certain piece of data, in this case the strin g 'five' should fail a certain piece of validation because it needs to be 5 (an int)

print ">>>>", value
bad_type = type(value).__name__
raise TypeError("expected numeric type for {0} but got '{1}'".format(column,bad_type))

prints:

.>>>> five
...
bad_type = type(value).__name__
TypeError: 'str' object is not callable

However I can do this from the command line:

python -c "print type('five').__name__"

prints

str

what am I doing wrong here? I want to print the type of the value that was passed and failed my custom validation.

Upvotes: 0

Views: 791

Answers (1)

Jon Clements
Jon Clements

Reputation: 142176

Are you sure you haven't over-ridden type somewhere?

Also, that's not the Pythonic way for type checking - instead use:

try:
    my_int = int(value)
except (ValueError, TypeError) as e:
    # raise something

Upvotes: 1

Related Questions