0

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 ;)

Community
  • 1
  • 1
srcolinas
  • 497
  • 5
  • 13
  • You might want to subclass `UserList` from `collections`, while this is no solution to your OP, I considered it important to note. – krysopath Jan 27 '17 at 13:24

1 Answers1

2

I am sorry..I tried and it turns out it is very straightforward. It was simply a mistake from the beggining. I leave the answer here in case someone needs it, but if you suggest I delete it (is it possible?) I'will do it.

class test(list):
    def __init__(self,pa, name, *args):
        list.__init__(self, *args)
        self.name = name
        self.par = pa

    def create(self):
        lista = []
        lista.append(test(self,'a',[0,1]))
        lista.append(test(self,'b',[0,2]))
        return lista

Best regards,

srcolinas
  • 497
  • 5
  • 13