简体   繁体   中英

Variable not the same type in two different functions

I have two functions which print into an excel file. THe only input is the file name. Here is the code:

#excelpy
import excelpy

#Tinker
from Tkinter import *
from tkSimpleDialog import *
from tkFileDialog import *

Function Mode1

def Mode1(full_name):
    print full_name
    print type(full_name) 
    testwbook = excelpy.workbook(full_name) 
    testwbook.show() 
    testwbook.set_cell((1,1),'TEST1', fontColor='red') 
    testwbook.set_range(2,1,['Number','Name']) 
    m1 = testwbook.save(full_name)
    testwbook.close()
    return m1

Function Mode2

def Mode2(full_name):
    print full_name
    print type(full_name) 
    testwbook = excelpy.workbook(full_name) 
    testwbook.show() 
    testwbook.set_cell((1,1),'TEST2', fontColor='red') 
    testwbook.set_range(2,1,['Number','Name']) 
    m2 = testwbook.save(full_name)
    testwbook.close()
    return m2

Main

root = Tk()
d = str(asksaveasfilename(parent=root,filetypes=[('Excel','*.xls')],title="Save report as..."))
d = d + '.xls'
d = d.replace('/','\\')
root.destroy()  

Mode1(d)
Mode2(d)

And once in a while I get the following error:

Traceback (most recent call last):
  File "T:\TEST\testpy.py", line 2035, in <module>
    Mode2(d)
  File ""T:\TEST\testpy.py"", line 1381, in Mode2
    print type(full_name) 
TypeError: 'str' object is not callable

Any idea why is this happening? How can I prevent it?

The only function call in the line you get the error is a call to the built-in function type() , so the only explanation for your error message is that you overwrote the built-in name type by a global name type pointing to a string object. Try adding

print type

before

print type(full_name)

It looks like somewhere you're setting a (global) variable named type to a string, thus overwriting the built-in type function.

Try searching your code for type = to see what turns up.

Understandably, Python would then throw that exception when you tried to call type (strings can't be "called").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM