Python сохранение значения как метода экземпляра вместо целого числа - PullRequest
0 голосов
/ 12 июля 2020

Итак, сейчас я пишу программу, которая случайным образом генерирует сеточный лабиринт. Следующий сегмент кода - это определение класса одной «ячейки» сетки. Я определил 2 метода получения 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_row, start_col, end_row и end_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_row и start_col позже в программа, я получаю сообщение об ошибке list indices must be integers, not instancemethod

Мой вопрос: почему start_row и start_col сохраняются как instancemethod, а не как целые числа?

1 Ответ

1 голос
/ 12 июля 2020

Вот небольшой фрагмент, иллюстрирующий различные варианты

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. 

В вашем случае я бы просто использовал член экземпляра publi c. Нет необходимости ни в одной из ваших функций. Фактически весь этот объект выглядит так, как будто это мог быть просто именованный кортеж :

from typing import NamedTuple
class Cell(NamedTuple):
    row: int
    col: int
    blocked: bool

или если вы используете python> = 3.8 a dataclass

from dataclasses import dataclass
@dataclass
class Cell:
    row = None
    col = None
    blocked = False
...