Home > Enterprise >  why does it say my array is not defined when i try and use it elseware in a calss? Python
why does it say my array is not defined when i try and use it elseware in a calss? Python

Time:05-26

class Grid:

def __init__(self, row, col, inp):
    self.row = row
    self.col = col
    self.inp = inp
    newGrid = []
    newGrid.append([[0]*col]*row)

def changeGrid(self, row, col, inp):
    newGrid #says it is not defined

Why cant changeGrid() access the variable newGrid?

CodePudding user response:

in order to access "newGrid" you need to make it an attribute of the class

try something like this

def __init__(self, row, col, inp):
    self.row = row
    self.col = col
    self.inp = inp
    self.newGrid = []
    self.newGrid.append([[0]*col]*row)

def changeGrid(self, row, col, inp):
    self.newGrid #now you should have access to it 

CodePudding user response:

Because newGrid is not reachable in the scope of changeGrid. Depending on what you're trying to achieve you could either make newGrid a class member or an instance member. More detail here Python Class Members

  • Related