I am working with a class and am trying to call a helper method from within the class. I got the following code to work, but I am unsure why I have to pass "self" as an argument to the helper function when I call it when I already have "self" as an argument in the method. Is there a reason that I have to pass it as an argument when I call Frequency.__helper(self, record) in the example below?
Thanks!
class Frequency:
def __init__(self, record):
self.record = record
def __helper(self, datalist)
do something to datalist...
def getFreq(self):
allrec = self.record
record = allrec[1].split(' ')
var = Frequency.__helper(self, record)
return var
The right way to call the method is just
var = self.__helper(record)
That does the same thing, but in a more intuitive fashion.
Yes, in this case you have to, because you're not declaring the function as an @staticmethod. When a method is not static, it requires an instance to be passed.
If you do something like:
class Frequency:
@staticmethod
def test(datalist):
pass
you'll not be required to define self into the argument list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With