473,388 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Curious string behavior

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
Jul 18 '05 #1
2 1209
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
Jul 18 '05 #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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dave Anderson | last post by:
Consider the following: var GUID = Server.CreateObject("Scriptlet.TypeLib").GUID Let's assume GUID is {9A46FCC9-A7A1-4C96-9394-B1A966CEC081}. I happened to notice that if I concatenate this...
35
by: Stan Milam | last post by:
The following code implements a strcat-like function which can receive a variable number of arguments. I thought it would be faster if I kept a pointer to the end of the string as it is built so...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
8
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.