Skip to content
Advertisement

how to pass variables to method

I want to pass variables to the method. which of the following is better? (x and y are constants)

class Class(Parent):
    def __init__(self):
        super().__init__()
        self.d = self.function()
    def function(self):
        return self.x + self.y

or:

class Class(Parent):
    def __init__(self):
        super().__init__()
        self.d = self.function(self.x,self.y)
    def function(self, x, y):
        return x + y

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement