I want to pass variables to the method. which of the following is better? (x and y are constants)
JavaScript
x
7
1
class Class(Parent):
2
def __init__(self):
3
super().__init__()
4
self.d = self.function()
5
def function(self):
6
return self.x + self.y
7
or:
JavaScript
1
7
1
class Class(Parent):
2
def __init__(self):
3
super().__init__()
4
self.d = self.function(self.x,self.y)
5
def function(self, x, y):
6
return x + y
7
Advertisement
Answer
I’d argue the second one is better with regards to function purity, though this is a principle of Functional Programming rather than Object Oriented Programming. The only thing I’d wonder then is why you’d even make it a member method at all, instead of a stand-alone function.