定义类只能从内部的ipython开始工作
我在文件夹定义类只能从内部的ipython开始工作,python,class,Python,Class,我在文件夹mypy中创建了一个类: class Shape: def __init__(self,x,y): self.x = x self.y = y def area(self) return self.x * self.y 如果我这样做: import sys sys.path.append('mypy') import Shape test = Shape(2,4) 我确实得到了一个错误: 'module' object
mypy
中创建了一个类:
class Shape:
def __init__(self,x,y):
self.x = x
self.y = y
def area(self)
return self.x * self.y
如果我这样做:
import sys
sys.path.append('mypy')
import Shape
test = Shape(2,4)
我确实得到了一个错误:
'module' object is not callable
如果我在
ipython
中定义类并调用它,那么一切都正常 在IPython中,提示的工作方式类似于单个模块,那么如果您定义一个类并尝试从中创建一个对象(如test=Shape(2,4))将起作用,因为您位于类定义的同一名称空间。导入模块形状时,也有一个名为Shape的已定义类,必须为类定义提供完整的限定名,如中所示:
import Shape
test = Shape.Shape(2,4) # test = Module.Class()
使用
test=Shape.Shape(2,4)
或从Shape导入Shape
。请看。这是wikibook的教程: