Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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将值保存为instancemethod而不是integer_Python_Python 2.7_Oop_A Star_Grid Search - Fatal编程技术网

Python将值保存为instancemethod而不是integer

Python将值保存为instancemethod而不是integer,python,python-2.7,oop,a-star,grid-search,Python,Python 2.7,Oop,A Star,Grid Search,所以,我现在正在写一个程序,随机生成一个网格迷宫。下面的代码段是网格中单个“单元”的类定义。我定义了两种getter方法get_row和get_col,它们可以检索单元格的坐标。我相信这就是问题所在 class cell: #Node storing each cell of grid def __init__(self, rowNum=None, colNum=None): self.rowNum = rowNum self.colNum = colNu

所以,我现在正在写一个程序,随机生成一个网格迷宫。下面的代码段是网格中单个“单元”的类定义。我定义了两种getter方法
get_row
get_col
,它们可以检索单元格的坐标。我相信这就是问题所在

class cell: #Node storing each cell of grid
    def __init__(self, rowNum=None, colNum=None):
        self.rowNum = rowNum
        self.colNum = colNum
        self.blocked = 0 # 0 denotes False, all cells are initially unblocked
        self.f = 0 # Distance from start node
        self.g = 0 # Distance from goal node
        self.h = 0 # Total cost ( h = f + g )
        self.parent = None
    def get_row(self):
        return self.rowNum
    def get_col(self):
        return self.colNum
    def isBlocked(self):
        if self.blocked == 0:
            return 0
        if self.blocked != 0:
            return 1
然后,我使用下面的线段遍历网格,并随机确定某些正方形作为起点和终点。我将这些坐标保存为
start\u row
start\u col
end\u row
end\u col

# List containing all the unblocked cells
unblockedCells = [cell() for i in range(numUnblocked)]

start_row = -1
start_col = -1
end_row = -1
end_col = -1

# Randomly picks 2 unblocked cells to be the starting and ending point
def set_start_and_end():
    global start_row
    global start_col
    global end_row
    global end_col
    ctr1 = 0
    for i in range(totalRows):
        for j in range(totalCols):
            if cellsArray[i][j].blocked == 0:
                unblockedCells[ctr1] = cellsArray[i][j]
                ctr1 = ctr1+1
    
    #Initialize start position
    startIndex = random.randint(0,len(unblockedCells)-1)
    start_row = unblockedCells[startIndex].get_row
    start_col = unblockedCells[startIndex].get_col

    #Initialize target position
    endIndex = random.randint(0, len(unblockedCells)-1)
    end_row = unblockedCells[endIndex].get_row
    end_col = unblockedCells[endIndex].get_col

    print("Start: (",start_row,", ",start_col,") End: (",end_row,", ",end_col,")")

set_start_and_end()
但是,当我稍后在程序中尝试访问
start\u row
start\u col
的值时,我收到错误
列表索引必须是整数,而不是instancemethod


我的问题是,为什么start\u行和start\u列被存储为instancemethod而不是整数?

这里有一个小片段来说明不同的选项

class Cell:
"""Doc strings go here btw, not as comments. Also take a look at pep8 naming conventions: https://www.python.org/dev/peps/pep-0008/#naming-conventions"""
    def __init__(self, row_num = None, col_num = None):
        self.row_num = row_num # A public instance member - you can get and set this directly without the need for function
        self._col_num = col_num # A private member. By convention, you should not get or set this directly
    def get_row(self):
    """This is a method, you need so call it using parenthesis i.e. .get_row()"""
        return self.row_num
    @property
    def col_num(self):
    """This is property being used to expose a private member. You don't need parenthesis to call it."""
        return self._col_num


my_cell = Cell(1, 2)
print(my_cell.row_num)  # row_num is a public member, you can just access it directly
print(my_cell.get_row)  # get_row is a method, so this will print the actual function
print(my_cell.get_row())  # this calls the function and prints the integer
print(my_call.col_num)  # because of the property decorator, we don't have to call this function but can treat is as if it was a member. But note it would be weird to name it get_... now as verbs imply functions. 
在您的情况下,我将只使用公共实例成员。您不需要任何功能。事实上,整个物体看起来就像是一个:

或者如果您使用的是python>=3.8a


unbuckedcells[startIndex].get\u row
是一个实例方法。您需要使用
()
准确地调用该方法,即
unchedcells[startIndex]。get_row()
是实例方法的返回值(即int)。或者,您可以使用
@属性来装饰getter,这样您就不需要
()
。尽管您的getter方法毫无意义,因为
self.rowNum
etc是公共成员。也不要使用
global
。此外,您的代码段中还有大量似乎未分配到任何位置的变量?谢谢,那么我是否应该改用unceedcells[startIndex].rowNum?你能解释一下如何使用@propertyse吗?请看我的答案。但是是的,您应该只使用
unbuckedcells[startIndex].rowNum
,因为这些函数没有意义。谢谢!这很有帮助
from typing import NamedTuple
class Cell(NamedTuple):
    row: int
    col: int
    blocked: bool
from dataclasses import dataclass
@dataclass
class Cell:
    row = None
    col = None
    blocked = False