This code given is a part of my program. Here I want to reduce the 3 functions to just 1 as they are exactly similar except 1 line. I read about passing function(let this function be Bar
) and its arguments as arguments in another function(let this function be called Foo
).
But in this scenario, I can't change the function Foo
. Here my Foo
function is .clicked.connect()
and addXMin
is my function Bar
. I want to pass Bar
and its argument num
into Foo
, where I can't change whats going on in Foo
. Is there a way I can reduce the 3 functions into 1 and pass 15
, 10
and 5
as arguments to that single function?
self.add15m.clicked.connect(self.add15Min)
self.add10m.clicked.connect(self.add10Min)
self.add5m.clicked.connect(self.add5Min)
def add15Min(self):
global mins, secs, time
time = self.lineEdit.text()
mins = int((time.split(':'))[0])
mins+=15 #The only different line
secs = int((time.split(':'))[1])
time = str(mins).zfill(2) + ":" + str(secs).zfill(2)
self.lineEdit.setText(time)
def add10Min(self):
global mins, secs, time
time = self.lineEdit.text()
mins = int((time.split(':'))[0])
mins+=10 #The only different line
secs = int((time.split(':'))[1])
time = str(mins).zfill(2) + ":" + str(secs).zfill(2)
self.lineEdit.setText(time)
def add5Min(self):
global mins, secs, time
time = self.lineEdit.text()
mins = int((time.split(':'))[0])
secs = int((time.split(':'))[1])
mins+=5 #The only different line
time = str(mins).zfill(2) + ":" + str(secs).zfill(2)
self.lineEdit.setText(time)