I have the following class:
class state(list):
def __init__(self,parent = None, path = None, *args):
list.__init__(self, *args)
self.n = len(self)
self.parent = parent
self.path = path
def neighbors(self):
lista = []
if self.__hasUp():
lista.append(state(Self,'Up',self.__swap(self.index(0),self.index(0) - 3))))
if self.__hasDown():
lista.append(state(Self,'Down',self.__swap(self.index(0),self.index(0) + 3))))
if self.__hasLeft():
lista.append(state(Self,'Left',self.__swap(self.index(0),self.index(0) - 1))))
if self.__hasRight():
lista.append(state(Self,'Right',self.__swap(self.index(0),self.index(0) + 1))))
return lista
I normally create an instance of the class as follow:
inicial = state(None,None,[1,2,5,3,4,0,6,7,8])
I need to do the followin (I think if I put the whole context of what I am doing, it will be missleading):
anotherList = []
for neighbor in inicial.neighbors():
anotherList.append(neighbor)
I just want to have a list with few extra attributes and methods. The problem is I need this class to create istances of itself with the object that is creating them as a parameter in the neighbors method. I have testet all methods referenced in the code and they work as expected, but I just think they are not need for this question and they will make it a really long question.
I have also checked this Declaring a class with an instance of it inside in Python, Create static instances of a class inside said class in Python and this Class constructor able to init with an instance of the same class object. However, I do not understand it yet :(
I am quite an amateur,so if you have any suggestions, they are all welcome ;)