1

I have a class. In method "Complete" i want to make a new instance of this class and add it to the array. But instead it just adds my current instance to the array. How can i create a new instance by using this method?

class Task:

    ArrPosition = 0

    def __init__(self, content, state, arr):
        self.content = content
        self.state = state
        self.arr = arr

    def Complete(self, Answer, CurrentPosition):
        if Answer == "True":
            self.state = "True"
            self.ArrPosition += 1
            if self.ArrPosition < 4:
                SessionArray[CurrentPosition + self.arr[self.ArrPosition]].append(Task(self.content, "NULL", [0,2,6,10]))
        if Answer == "False":
            self.state = "True"
            self.ArrPosition = 0
            SessionArray[CurrentPosition + 1].append(Task(self.content, "NULL", [0,2,6,10]))

Other examples show how to do that outside of the class description, but i need to do that inside.

FrostahG
  • 11
  • 2
  • 5
    Possible duplicate of [Create new class instance from class method](https://stackoverflow.com/questions/13260557/create-new-class-instance-from-class-method) – paltaa Oct 01 '19 at 14:38
  • I'm not sure, cause there is a case to create a copy outside of class description, but i need to do that inside – FrostahG Oct 01 '19 at 14:47
  • There's nothing special about creating a new instance from within a method of that class - you're definitely appending new instances of `Task` to your array. Unfortunately, you haven't given us enough of a [mcve] to be able to tell what's going wrong. The one thing that looks suspicious in the code you did post is `ArrPosition` - you initialize it as a class attribute, but then override it with an instance attribute, so perhaps it doesn't have the value you expect in the new instance. – jasonharper Oct 01 '19 at 15:24
  • Added a new answer. It helped me:D – FrostahG Oct 01 '19 at 15:58

2 Answers2

0

Solved it with

class Task:

    ArrPosition = 0

    def __init__(self, ArrPosition, content, state, arr):
        self.ArrPosition = ArrPosition
        self.content = content
        self.state = state
        self.arr = arr

    def Clone(self):
        return Task(self.ArrPosition, self.content, "NULL", self.arr)

    def Complete(self, Answer, CurrentPosition):
        if Answer == "True":
            self.state = "True"
            self.ArrPosition += 1
            if self.ArrPosition < 4:
                SessionArray[CurrentPosition + self.arr[self.ArrPosition]].append(Task.Clone(self))
        if Answer == "False":
            self.state = "False"
            self.ArrPosition = 0
            SessionArray[CurrentPosition + 1].append(Task.Clone(self))
FrostahG
  • 11
  • 2
-1

I checked the script on my console without changing anything and it worked perfectly fine with some arbitrary values chosen for the Task constructor, for the arguments of the Complete method and for the SessionArray variable:

enter image description here

The cause to your problem could be somwhere else in your code, possibly some erratic input you're feeding it.

Michele Bastione
  • 363
  • 3
  • 13
  • The problem happens when it works on big datasets. I solved it with the method which i've written upper. Thanks! – FrostahG Oct 01 '19 at 15:57