私は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をインポートしたときとPointimport*から行ったときで動作が異なる理由です。どちらのインポート方法が良いスタイルですか?
また、クラスとファイル名に関連性はありますか?