Curious string behavior

Discussion in 'Python' started by mark, Jan 28, 2004.

  1. mark

    mark Guest

    I've encountered an anomaly while using the string module (specifically,
    string.split). Here's the snippet:

    import string

    address2 = ' '
    line = 'function, dealer, type, firstname, lastname, vin, blank'
    print 'Address2 Type (first check): ', type(address2)
    function, dealer, type, firstname, lastname, vin, blank =
    string.split(line, ',')
    print 'Address2 type (second check): ', type(address2)

    I've extracted this from a larger script, but the error happens roughly
    the same way. Now, here's what I would expect to see:

    Address2 Type (first check): <type 'str'>
    Address2 type (second check): <type 'str'>

    Here's what I get instead:

    Address2 Type (first check): <type 'str'>
    Address2 type (second check):
    Traceback (most recent call last):
    File
    "C:\PROGRA~1\Python\lib\site-packages\Pythonwin\pywin\framework\scriptut
    ils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
    File "C:\Program Files\Python\test1.py", line 7, in ?
    print 'Address2 type (second check): ', type(address2)
    TypeError: 'str' object is not callable

    What the heck is going on here? I figure I'm just missing something.

    - Mark Daley
    Product Manager
    Diversiform, Inc.
    1-800-444-3445
    mark, Jan 28, 2004
    #1
    1. Advertising

  2. > address2 = ' '
    > line = 'function, dealer, type, firstname, lastname, vin, blank'
    > print 'Address2 Type (first check): ', type(address2)
    > function, dealer, type, firstname, lastname, vin, blank =

    ^^^^^
    Here you rebind the name type (which is initially bound to <type 'type'>,
    that can be called with an argument to get its type) to the string ' '.
    Hence the second

    type(address2)

    doesn't work.

    You can do this on all symbols/names in python, which can cause confusing
    errors.

    Diez
    Diez B. Roggisch, Jan 28, 2004
    #2
    1. Advertising

  3. mark wrote:

    > address2 = ' '
    > line = 'function, dealer, type, firstname, lastname, vin, blank'
    > print 'Address2 Type (first check): ', type(address2)
    > function, dealer, type, firstname, lastname, vin, blank =
    > # ^ LINE ABOVE CONTAINS ERROR
    > string.split(line, ',')
    > print 'Address2 type (second check): ', type(address2)

    <snip>
    > Address2 Type (first check): <type 'str'>
    > Address2 type (second check):
    > Traceback (most recent call last):
    > File
    > "C:\PROGRA~1\Python\lib\site-packages\Pythonwin\pywin\framework\scri
    > ptut ils.py", line 310, in RunScript
    > exec codeObject in __main__.__dict__
    > File "C:\Program Files\Python\test1.py", line 7, in ?
    > print 'Address2 type (second check): ', type(address2)
    > TypeError: 'str' object is not callable


    In the marked line, you assign a string to a variable called
    "type". This means that you overwrote your reference to the type
    function that you are trying to use in type(address2). When you "call"
    type(address2) you are now trying to call the string which you have
    assigned to the name "type". Use a different name for your variable (a
    name that the library doesn't use) and you will be fine.

    --
    --OKB (not okblacke)
    "Do not follow where the path may lead. Go, instead, where there is
    no path, and leave a trail."
    --author unknown
    OKB (not okblacke), Jan 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    433
  2. BartlebyScrivener

    curious paramstyle qmark behavior

    BartlebyScrivener, Oct 20, 2006, in forum: Python
    Replies:
    7
    Views:
    347
    Jon Clements
    Oct 21, 2006
  3. Jim B. Wilson

    The curious behavior of integer objects

    Jim B. Wilson, Jan 15, 2007, in forum: Python
    Replies:
    15
    Views:
    392
    Carl Banks
    Jan 16, 2007
  4. Matthew Franz

    Curious UnboundLocalError Behavior

    Matthew Franz, Feb 28, 2007, in forum: Python
    Replies:
    2
    Views:
    304
    Matthew Franz
    Mar 1, 2007
  5. Boris Borcic
    Replies:
    3
    Views:
    376
    Raymond Hettinger
    Apr 21, 2007
Loading...

Share This Page