我有一个名为 Point2.py 的 python 文件,其中包含以下代码
class Point():
def __init__(self,x=0,y=0):
self.x = x
self.y = y
def __str__(self):
return "%d,%d" %(self.x,self.y)
现在在解释器中,我这样做了:
>>> import Point2
>>> p1 = Point()
但我收到一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
Point2.py 文件中有 Point 类。为什么我无法将其分配给 p1。当我尝试时:
>>> from Point import *
>>> p1 = Point()
有用
我将文件重命名为 Point.py 然后我做了
>>> import Point
>>> p1 = Point
这可行,但分配值并不容易。
然而,
>>> from Point import *
>>> p1 = Point(3,4)
作品。
我的问题是为什么当我导入 Point 和从 Point import *. 哪种导入方式好?
类和文件名有什么关系吗?