我已经从指南中复制了一个python代码:
class Carta:
ListaSemi=["Fiori","Quadri","Cuori","Picche"]
ListaRanghi=["impossibile","Asso","2","3","4","5","6",\
"7","8","9","10","Jack","Regina","Re"]
def __init__(self, Seme=0, Rango=0):
self.Seme=Seme
self.Rango=Rango
def __str__(self):
return (self.ListaRanghi[self.Rango] + " di " + self.ListaSemi[self.Seme])
def __cmp__(self, Altro):
#controlla il seme
if self.Seme > Altro.Seme: return 1
if self.Seme < Altro.Seme: return -1
#se i semi sono uguali controlla il rango
if self.Rango > Altro.Rango: return 1
if self.Rango < Altro.Rango: return -1
return 0
当我从shell打电话时:
>>> Carta1=Carta(1,11)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
我使用的是python 2.7版。 怎么了? 感谢
答案 0 :(得分:3)
我假设上面的代码段保存为Carta.py
,并且您在交互式shell中运行:
>>> import Carta
>>> Carta1=Carta(1,11)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
这样,您尝试调用/实例化模块而不是其中的类。您基本上有两个选项可以解决此问题,更改导入或构造函数调用:
>>> from Carta import Carta
>>> Carta1=Carta(1,11)
>>> import Carta
>>> Carta1=Carta.Carta(1,11)
如果您重命名模块文件或类,以便更好地区分它们,那么它就会变得清晰。