简体   繁体   English

两种不同功能中的变量类型不同

[英]Variable not the same type in two different functions

I have two functions which print into an excel file. 我有两个功能可以打印到excel文件中。 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 功能模式1

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 功能模式2

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. 发生错误的行中唯一的函数调用是对内置函数type()的调用,因此,对错误消息的唯一解释是,您用指向一个字符串的全局名称type覆盖了内置名称type 。字符串对象。 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. 看起来您在某处(将一个名为type的(全局)变量设置为字符串)覆盖了内置的type函数。

Try searching your code for type = to see what turns up. 尝试在代码中搜索type =以查看结果。

Understandably, Python would then throw that exception when you tried to call type (strings can't be "called"). 可以理解,当您尝试调用type (字符串不能被“调用”)时,Python会抛出该异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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