chrise
chrise

Reputation: 4253

type() in python 2.5 returning error when passing in string

Using Python 2.5.2 (r252:60911, Sep 30 2008, 15:42:03) I try to use

type(x) 

and receive the following error when passing in this string '2014-02-06 00:00:00'

TypeError: 'str' object is not callable

why does it not return str?

Upvotes: 1

Views: 86

Answers (1)

SethMMorton
SethMMorton

Reputation: 48745

You probably shadowed the python function type with a string by doing the following:

>>> type = 'some string'
>>> # Lots of code
>>> x = 'other string'
>>> type(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

You can fix this by not using variable names that are also python builtin functions.


If you absolutely cannot rename your variable to be something other than type (_type or type_ will do), you can access the builtin function using the __builtin__ module

>>> type = 'some string'
>>> # Lots of code
>>> x = 'other string'
>>> import __builtin__
>>> __builtin__.type(x)
<type 'str'>

Anyone reading your code will hate you, though.

Upvotes: 6

Related Questions