0

I want only the 'self.news' variable to be reset or to get a new value in the class I created below.but when I run the three function. All values ​​return to their original state. and the functions I used before are invalid.How do I change or reset only variable 'self.nexts'. very very veyr thank you.

class new:
    def __init__(self, name=None, nexts=None):
        self.nexts = nexts
        self.name = name

    def one(self):
        self.nexts = 2
        return self.nexts

    def two(self):
        self.nexts = 1
        self.name = 'What'
        return self.nexts

    def three(self):
        self.__init__()
        print(self.name)
        print(self.nexts)


a = new()

print(a.one())
#output = 2
print(a.two())
#output = 1, hate
a.three()
#output = None, None
sabcan
  • 407
  • 3
  • 15
  • 1
    You should call __init__ with the vaues you want the instance variables to be reset to i.e. `self.__init__(None, self.nexts)` as discussed in [How to call the constructor from within member function](https://stackoverflow.com/questions/25118798/python-how-to-call-the-constructor-from-within-member-function) – DarrylG Oct 17 '20 at 12:33

1 Answers1

2

You can init with the vaues you want the instance variables to be reset to as described by: How to call the constructor from within member function

def three(self):
    self.__init__(None, self.nexts)
    print(self.name)
    print(self.nexts)

a.three()
# Output: None, 1
DarrylG
  • 16,732
  • 2
  • 17
  • 23