1

I wonder is there any function to set variable as default parent value?

Here's the parent class

class Testback:
    def __init__(self, testbackway,data,result,statusCode=2,store=0,tradeCounter=0,sellCounter=0,
        totalP=0,totalM=0,totalF=0,totalT=0,keepDay=0,win=0,fail=0,fee=45):
        self.testbackway = testbackway
        self.data = data
        self.result = result
        self.statusCode = statusCode
        self.store = store
        self.tradeCounter = tradeCounter
        self.sellCounter = sellCounter
        self.totalP = totalP
        self.totalM = totalM
        self.totalF = totalF
        self.totalT = totalT
        self.keepDay = keepDay
        self.win = win
        self.fail = fail
        self.fee = fee

I want to initialize these variable in children class as default every time in for loop and I do it one by one now.

def start(self):
    for i in range(1,33):
        for j in range(i+1,33):
            print(i,j)
            for d in range(1,50):
                # status code
                self.statusCode = 2
                self.store = 0
                self.tradeCounter = 0
                self.sellCounter = 0
                self.totalP = 0
                self.totalM = 0
                self.totalF = 0
                self.totalT = 0
                self.keepDay = 0
                # 輸贏
                self.win = 0
                self.fail = 0
                self.currsive(i,j,d)

Is there any smarter way to do that?

Hsuan Hsu
  • 69
  • 4
  • 1
    @juanpa I guess `self.currsive(i,j,d)` changes the attributes, so OP wants to reset them each time. – wjandrea Jun 18 '21 at 18:43
  • Does this answer your question? [Get a function argument's default value?](/q/12627118/4518341), i.e. `get_default_args(super().__init__)` – wjandrea Jun 18 '21 at 21:22

1 Answers1

1

you can create a method that does it

def initialize():
    self.statusCode = 0
    self.store = 0
    ...
    
for d in range(1,50):
    self.initialize()
fthomson
  • 773
  • 3
  • 9
  • I used this method before but actually my variables are more than 50. I have to check this method every time when I add something new and it could be missed. So I think maybe there's already build-in function in python to use. – Hsuan Hsu Jun 18 '21 at 18:55
  • The purpose of the method would be to re-initialize the values you want set to zero. Are you trying to assign 50 attributes to zero? – fthomson Jun 18 '21 at 19:08
  • Not setting all values to 0, like the default of self.statusCode is 2, self.fee is 45. – Hsuan Hsu Jun 18 '21 at 19:40
  • I see. I would suggest re-factoring your code to avoid having that many instance attributes. One idea would be to to group all of your attributes into a dictionary or dataframe/series and pass that into the class. That way you maintain the original values but can easily create a copy as needed. You could try re-calling the init inside the class or some other craziness but this is highly unadvisable. I can update my answer if needed. – fthomson Jun 18 '21 at 20:14