"len()" gives a curious error

Steve Holden sholden at holdenweb.com
Wed Mar 5 15:39:18 EST 2003


"Mike McKernan" <mck at pobox.com> wrote in message
news:udfzq1ef33.fsf at tivoli.mv.com...
> Hi List,
>
> I am an admirer of Python, but only an occasional user, so I may
> easily be overlooking something.  Here's the story.
>
> I just wrote a quicky script to convert ip address ranges in the
> masked form (192.168.231.64/23 e.g.) to the first address - last
> address form (192.168.230.0 - 192.168.231.255), which worked fine
> until I added a few checks.
>
> The check that's bothering me is trying to confirm that there are
> exactly four elements in the dotted quad address.  This line should
> produce a list of the individual elements:
>
>         addr = string.split(addr,".")
>
> and     print type(addr)
>
> does indeed produce <type 'list'>
>
> but     print len(addr)
>
> gives the following:
>
>         Traceback (most recent call last):
>           File "/u/mck/bin/ipaddr", line 27, in ?
>             print len(addr)
>         TypeError: 'str' object is not callable
>
> Curiously, everything works as expected if I run interactively.
>
Read the error message: you are trying to apply a function (len) to an
argument (addr), and the interpreter is telling you that "len" isn't bound
to something you can call, it's bound to a string. Perhps it could try a
little harder to tell you that, but that's what it's actually saying.

Sure enough, working on that theory I find in your script:

> addr, len = string.split(args[0],"/")

which I declare to be the culprit. Use some name other than "len" there...

regards

> What's going on?
>
I hope you now know :-)

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/
Register for PyCon now!            http://www.python.org/pycon/reg.html







More information about the Python-list mailing list