Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,哪种导入方式更好,以及对类对象的一些说明_Python_Class_Object_Import - Fatal编程技术网

在python中,哪种导入方式更好,以及对类对象的一些说明

在python中,哪种导入方式更好,以及对类对象的一些说明,python,class,object,import,Python,Class,Object,Import,我有一个名为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(

我有一个名为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
它起作用了

我将该文件重命名为Point.py,然后我这样做了

>>> import Point
>>> p1 = Point 
这是可行的,但赋值并不容易

但是,

>>> from Point import *
>>> p1 = Point(3,4)  
工作

我的问题是,为什么导入点和从点导入时,它的行为不同*。哪种导入方式是好的样式

类和文件名是否有关联?

回答原因:

>>> import Point2
>>> p1 = Point()
不起作用,是因为您没有直接导入类Point()。要访问
Point()
类,您需要执行以下操作:

>>> import Point2
>>> p1 = Point2.Point()

从x导入执行
*
导入模块中的所有内容。因此,如果模块
bar
中有一个函数
foo()
,那么从bar执行
导入foo
将导入该函数,这样您就可以执行
foo()
,而不必执行
bar.foo()
。这就是为什么当您从Point import*执行
操作时,它的工作方式与您不需要执行的
Point.Point()
相同


进口哪种方式更好?嗯,除非您打算使用
x
中的所有内容,否则不建议从x导入执行
*
。它也可能与您自己脚本中的函数混淆

使用从x导入y的
或导入x的
查看。

回答原因:

>>> import Point2
>>> p1 = Point()
不起作用,是因为您没有直接导入类Point()。要访问
Point()
类,您需要执行以下操作:

>>> import Point2
>>> p1 = Point2.Point()

从x导入执行
*
导入模块中的所有内容。因此,如果模块
bar
中有一个函数
foo()
,那么从bar执行
导入foo
将导入该函数,这样您就可以执行
foo()
,而不必执行
bar.foo()
。这就是为什么当您从Point import*
执行
操作时,它的工作方式与您不需要执行的
Point.Point()
相同


进口哪种方式更好?嗯,除非您打算使用
x
中的所有内容,否则不建议从x导入执行
*
。它也可能与您自己脚本中的函数混淆


使用
从x导入y
导入x
,看一看。

使用
从点2导入点
,它不会像
*
那样导入所有内容。查看更多关于Haidro的信息。从第2点导入点使用
,它不会像
*
那样导入所有内容。更多关于海德罗的信息。谢谢,我现在明白了。我还有一个分支问题是汉克斯,我现在明白了。另一个分支问题是,如果我在Point类中将一个方法定义为def add(self,other)和def_uadd(self,other),会有什么区别。i、 这里的区别是什么,def\u add\u(self,other):返回“%d,%d%”(self.x+other.x,self.y+other.y)def addtwo(self,other):返回“%d,%d%”(self.x+other.x,self.y+other.y)并且如何使用它们会很有帮助。我不相信有什么区别(我自己对类不是很有经验)但是要使用它们,你可以使用
a=Point()
a.\uuuu添加(x)
其中x是/是你的参数/s。在你的例子中,你应该返回数字,而不是用于添加的字符串。
def\uu添加(self,other):返回(self.x+other+x,self.y+other.y)
实际上,也许你应该返回一个带有新值的新点()。这我不确定。谢谢,我现在明白了。我还有一个分支问题是Hanks,我现在明白了。另一个分支问题是,如果我在Point类中将一个方法定义为def add(self,other)和def_uadd(self,other)有什么区别例如,这里的区别是什么,def\u add\u(self,other):返回“%d,%d%”(self.x+other.x,self.y+other.y)def addtwo(self,other):返回“%d,%d%”(self.x+other.x,self.y+other.y)并且如何使用它们会非常有用。我不相信有什么区别(我自己对类不是很有经验)但是要使用它们,你可以使用
a=Point()
a.\uuuu添加(x)
其中x是/是你的参数/s。在你的例子中,你应该返回数字,而不是用于添加的字符串。
def\uu添加(self,other):返回(self.x+other+x,self.y+other.y)
实际上,也许您应该返回一个带有新值的新点()。我不确定。