开发者

Variable not the same type in two different functions

开发者 https://www.devze.com 2023-03-23 16:37 出处:网络
I have two functions which print into an excel file. THe only input is the file name. Here is the code:

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(asksaveasfil开发者_如何学Pythonename(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").

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号