How to extract digit from a number?

Roy Smith roy at panix.com
Mon Jul 21 16:53:10 EDT 2014


In article <d84aa18f-7c12-4262-a746-218581783304 at googlegroups.com>,
 fl <rxjwg98 at gmail.com> wrote:

> >>> a = 1234
> >>> [int(d) for d in str(a)] 
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in <module>
> TypeError: 'str' object is not callable

This looks like you've overwritten str with an *instance* of a string.

When a python interpreter starts up, there are certain pre-defined 
symbols.  For example, 'str' is bound to the string class.  But, there's 
nothing to prevent you from re-defining it.  Here's an example which 
produces the same result you got:

---------------------------------------------------------------------
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on 
darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> str = "foo"
>>> a = 1234
>>> [int(d) for d in str(a)] 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
---------------------------------------------------------------------



More information about the Python-list mailing list