Dean Christian Armada Dean Christian Armada - 29 days ago 17
Python Question

self parameter error celery

I saw some blogs online where they put self parameters on their celery functions, why is mine causing an error like:

TypeError: xml_gr() takes exactly 1 argument (0 given)


Here is my code:

@periodic_task(run_every=timedelta(seconds=5))
def xml_gr(self):
ftp = FTP('xxxxx')
ftp.login(user='xxxxx', passwd='xxxxx')
x = xml_operation('AGIN', GR_GLOBAL_CURRENT_DATE, ftp, "GR")
ftp.close()

Answer

"self" is used within class member functions. When you call a member function in an instance of the class, the language automatically passes in the class instance as "self". "self" lets you access the members of the class.

class Thing:
    var1 = None
    def set_member(self, value):
        self.var1 = value

    def show_member(self, value):
        return self.var1

Then usage would be

a = Thing()
a.set_member(23)
a.show_member()

And you'd see the response 23. You don't have to pass in the "self" variable explicitly.

When you declare functions outside of a class, there is no reason to use "self".

Comments