Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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 - Fatal编程技术网

Python 类并定义属性的类型

Python 类并定义属性的类型,python,Python,我是Python新手,对Python 2.6中的类及其属性有疑问 假设我有一个类节点,如下所示: class Node: x = int y = int neighbours = [] discovered = bool def __init__(self,x,y): self.x = x self.y = y def setNeigbours(self,neighbours): self.neig

我是Python新手,对Python 2.6中的类及其属性有疑问

假设我有一个类
节点
,如下所示:

class Node:
    x = int
    y = int
    neighbours = []
    discovered = bool
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def setNeigbours(self,neighbours):
        self.neighbours = neighbours

    def addNeighbours(self,n):
        if type(n) == list:
            self.neighbours.extend(n)
        else:
            self.neighbours.append(n)

    def isDiscovered(self):
        return self.discovered

    def setDiscovered(self,discovered):
        self.discovered = discovered
class Graph:
    nodeList = Node
    edgeList = Edge

    def __init__(self,nodeList,edgeList):
        self.edgeList = edgeList
        self.nodeList = nodeList

    def getNodes(self):
        return self.nodeList

    def setNodes(self,nodeList):
        self.nodeList = nodeList

    def addNode(self,n):
        if type(n) == list:
            self.nodeList.extend(n)
        else:
            self.nodeList.append(n)
foo = 1 # this is an integer
foo = "string" # this is a string
还有一个类
,如下所示:

class Node:
    x = int
    y = int
    neighbours = []
    discovered = bool
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def setNeigbours(self,neighbours):
        self.neighbours = neighbours

    def addNeighbours(self,n):
        if type(n) == list:
            self.neighbours.extend(n)
        else:
            self.neighbours.append(n)

    def isDiscovered(self):
        return self.discovered

    def setDiscovered(self,discovered):
        self.discovered = discovered
class Graph:
    nodeList = Node
    edgeList = Edge

    def __init__(self,nodeList,edgeList):
        self.edgeList = edgeList
        self.nodeList = nodeList

    def getNodes(self):
        return self.nodeList

    def setNodes(self,nodeList):
        self.nodeList = nodeList

    def addNode(self,n):
        if type(n) == list:
            self.nodeList.extend(n)
        else:
            self.nodeList.append(n)
foo = 1 # this is an integer
foo = "string" # this is a string
如果我现在创建一个新图形并添加一些节点:

n1 = Node(0,0)
n2 = Node(1,1)
g = Graph([],[])
g.addNode(n1)
g.addNode(n2)
我希望打印以下内容:

但当我运行它时,我得到:

<type 'instance'>
<type 'instance'>


有没有办法告诉Python,
nodeList
是一个列表,它只包含
Node
-元素

这些都不是编写Python的方式;它看起来像Java

您不需要在类级别“声明”属性;这样做的唯一原因是使用在所有实例之间共享的值,而您没有这样做,因为您会立即用实例属性覆盖它们。删除这些定义

编写getter和setter方法也是非常不习惯的,特别是当它们只返回属性时。您的调用代码应该直接访问属性

至于您的问题,因为您使用的是Python 2[*],所以您的类必须从对象继承。如果这样做,您将看到正确的类型:

class Graph(object):
   ...

g = Graph()
type(g) # module.Graph

(*)注意,您确实不能使用2.6;它已过时,不受支持。如果你被卡在2.x上,至少使用2.7;但实际上,您应该使用3.x,它除了可以修复您的原始问题外,还可以修复您的原始问题。

如果您使用的是最新版本的Python(在编写本文时是3.6.0),那么确实可以

Python本身实际上不会对这些信息做任何处理,但是Python有一些类型检查工具,这些工具将执行静态分析,并告诉您是否尝试将
节点以外的内容放入列表中

对于较旧版本的Python 3,您可以使用格式化注释执行类似操作,然后类型检查工具将能够执行相同的检查:

from typing import List

class Graph:
    nodeList  # type: List[Node]
当然,所有这些在运行程序之前只提供可选的静态类型检查。一旦代码运行,Python就不会关心对象的类型,只要它们有正确的方法和属性:如果它像鸭子一样嘎嘎作响,Python会把它当作鸭子对待

如果您想了解更多关于Python中可选静态类型检查的信息,请参阅

打印
节点
类型的实例会给您提供
,这表明您正在使用某种版本的Python 2。如果可能的话,请考虑切换到一个较新的版本,但至少如果您想继续使用Python 2,请确保所有的对象都明确地从<代码>对象< /代码>:

class Node(object):
   ...

如果这样做,print语句将显示类。如果做不到这一点,您将得到Python早期的老式类,您会发现属性之类的东西不能正确使用它们。

在Python中无法强制使用类型,因为Python是动态类型的

一种可能的解决方案是在运行时检查实例类型

首先,从
对象继承类(即“新样式”对象):

然后,您可以在运行时执行类型检查:

>>> g = Graph()
>>> type(g)
<class '__main__.Graph'>
>>> isinstance(g, Graph)
True
>g=Graph()
>>>类型(g)
>>>isinstance(g,图)
符合事实的

Python是一种动态类型化语言。您不需要在使用变量之前声明它们,也不需要声明它们的类型

你可以这样做:

class Node:
    x = int
    y = int
    neighbours = []
    discovered = bool
    def __init__(self,x,y):
        self.x = x
        self.y = y

    def setNeigbours(self,neighbours):
        self.neighbours = neighbours

    def addNeighbours(self,n):
        if type(n) == list:
            self.neighbours.extend(n)
        else:
            self.neighbours.append(n)

    def isDiscovered(self):
        return self.discovered

    def setDiscovered(self,discovered):
        self.discovered = discovered
class Graph:
    nodeList = Node
    edgeList = Edge

    def __init__(self,nodeList,edgeList):
        self.edgeList = edgeList
        self.nodeList = nodeList

    def getNodes(self):
        return self.nodeList

    def setNodes(self,nodeList):
        self.nodeList = nodeList

    def addNode(self,n):
        if type(n) == list:
            self.nodeList.extend(n)
        else:
            self.nodeList.append(n)
foo = 1 # this is an integer
foo = "string" # this is a string
变量
foo
将具有存储在其中的值的类型

在Python中,您可以在同一个列表中存储不同类型的对象,如果您只想强制存储一种类型,则可以对内置的
list
类进行子类化