我正在编写面向3.2及更高版本的Python。看起来使用内置函数callable是最直接、最有效的方法。我看过关于hasattr(x,"__call__")、collections.Callable(x)的建议,并且只使用try/except尝试调用。我已经测试了可调用的项目(一个类和一个函数),使用timeit进行了100,000次迭代;在这两种情况下,使用callable只需要大约75%的时间来检查属性。当项目不可调用(整数和字符串)时,使用callable的成本与类或函数相同,而检查属性的成本约为类或函数的2.3倍。我没想到会有这种差异,但它也有利于清晰简洁的callable(x
我有一个接受用户输入的Python程序。我将用户输入存储在一个名为“userInput”的字符串变量中。我希望能够调用用户输入的字符串...userInput=input("Enteracommand:")userInput()由此,我得到错误:TypeError:'str'objectisnotcallable目前,我的程序正在做这样的事情:userInput=input("Enteracommand:")ifuserInput=='example_command':example_command()defexample_command():print('HelloWorld!')显
我正在使用python和schedulelib创建一个类似cron的作业classMyClass:deflocal(self,command):#returnsubprocess.call(command,shell=True)print"local"defsched_local(self,script_path,cron_definition):importscheduleimporttime#job=self.local(script_path)schedule.every(1).minutes.do(self.local(script_path))whileTrue:schedu
这个问题在这里已经有了答案:TypeError:'int'objectisnotcallable(10个答案)关闭3年前。我想弄清楚为什么在范围内使用求和函数时会出错。代码如下:data1=range(0,1000,3)data2=range(0,1000,5)data3=list(set(data1+data2))#makesnewlistwithoutduplicatestotal=sum(data3)#calculatesumofdata3list'selementsprinttotal这里是错误:line8,intotal2=sum(data3)TypeError:'int'o
我有一些使用call_later使用Python3.4的asyncio制作的简单代码。代码应该打印,等待10秒,然后再次打印(但是在应该执行end()时引发TypeError,见下文):importasyncio@asyncio.coroutinedefbegin():print("Startingtowait.")asyncio.get_event_loop().call_later(10,end())@asyncio.coroutinedefend():print("completed")if__name__=="__main__":try:loop=asyncio.get_eve
我正在实现一个简单的类来表示二维向量。以下是相关部分:classVector:def__init__(self,x,y):self.vec_repr=x,ydef__add__(self,other):new_x=self.x+other.xnew_y=self.y+other.yreturnVector(new_x,new_y)def__getattr__(self,name):ifname=="x":returnself.vec_repr[0]elifname=="y":returnself.vec_repr[1]稍后,我有类似的东西:a=Vector(1,1)b=Vector(2
我正在使用EclipseKepler(2013)和python3.3.2并运行一个简单的导入importgloba=glob.glob('*')print(a)给出一个:TypeError:'module'objectisnotcallable如果我在Idle中运行相同的代码,情况就不同了。我知道我错过了一些东西。感谢任何帮助。 最佳答案 对我有用的是我将文件顶部的importglob更改为fromglobimportglob。 关于python-类型错误:'module'objecti
我想修改Flask-Login中的is_active,这样用户就不会一直处于事件状态。默认值始终返回True,但我将其更改为返回banned列的值。根据文档,is_active应该是一个属性。但是,内部Flask-Login代码引发:TypeError:'bool'objectisnotcallable尝试使用is_active时。如何正确使用is_active来停用某些用户?classUser(UserMixin,db.Model):id=db.Column(db.Integer,primary_key=True)banned=db.Column(db.Boolean,default
我试图从django网站上学习django教程,但我遇到了一些问题:我必须将我的__unicode__方法添加到我的模型类中,但是每当我尝试返回该模型的对象我收到以下错误:in__unicode__returnself.question()TypeError:'unicode'objectisnotcallable我是python的新手,也是django的新手,我真的看不出我在这里错过了什么,如果有人能指出,我将不胜感激。一些代码:我的模型.py:#Thecodeisstraightforward.Eachmodelisrepresentedbyaclassthatsubclasses
这个问题在这里已经有了答案:Python:ModuleErrorwithpprint,noerrorwithprint(1个回答)关闭4个月前。我试过这段代码来漂亮地打印一个dict:importpprintpprint({})这会引发以下错误:Traceback(mostrecentcalllast):File"temp.py",line3,inpprint({})TypeError:'module'objectisnotcallable为什么它不可调用?