0

In the below code I am getting NameError: name 'checkNeighbour' is not defined. I am not able to understand when to use self keyword so I wanted to know the following:

1) How to call one function inside another. Usually, in Java you would directly call the function, here also I tried calling it directly as checkNeighbour(image,sr,sc,newColor, visited) instead of self.checkNeighbour(image,sr,sc,newColor, visited) but it gave the same error.

2) When recursing, will I have to use self.checkNeighbour(image, x,y,newColor,visited) or checkNeighbour(image, x,y,newColor,visited)

3) How to use the global variable movement inside the checkNeighbour() function, i.e with or without self

class Solution:
    movement = [0,-1,0,1,0]

def checkNeighbour(self,image : List[List[int]],sr:int,sc:int,newColor:int, visited:List[List[int]]) -> None:
    previousColor = image[sr][sc]
    image[sr][sc] = newColor
    visited[sr][sc] = -1

    for i in range(4):
        x = sr + self.movement[i]
        y = sc + self.movement[i+1]

        if(x < 0 or y < 0 or x >= len(image) or y >= len(image[0])):
            continue

        if(image[x][y] == previousColor and visited[x][y] != -1):
            checkNeighbour(image, x,y,newColor,visited) //recurse the function


def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
    visited = [[0 for i in range(len(image[0]))] for j in range(len(image))]
    self.checkNeighbour(image,sr,sc,newColor, visited)

    return(image)
Kushagra
  • 41
  • 1
  • 1
  • 5
  • Take a look [here](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self). – Diggy. May 11 '20 at 23:15
  • Have you done **any research at all**? For example, have you read the [documentation/tutorial](https://docs.python.org/3/tutorial/classes.html) on python classes? Note, Python != Java. Have you looked up any other stack overflow questions on `self` in Python? – juanpa.arrivillaga May 11 '20 at 23:24
  • Note, `movement` is **not a global variable** – juanpa.arrivillaga May 11 '20 at 23:24
  • You should have an `__init__(self)` method and assign variables in that use `self`. The way you assigned you need to use `Solution.movement` – bherbruck May 11 '20 at 23:26
  • 1
    Since you are new to Python is there a reason you have type annotations in your code (i.e. List, int)? – DarrylG May 11 '20 at 23:26
  • @TenaciousB--actually you can use class attributes via instance or via the class name. – DarrylG May 11 '20 at 23:31

0 Answers0