Bình Ông
Bình Ông

Reputation: 96

Call other function in staticmethod in Python

I just start using Python to create new project but I have problems about Class, Static Method in Python.

I want to create a Utils class that I can call from other place so I create this class below but I can not call other function in this class without self

import json
class Utils:

    @staticmethod
    def checkRequestIsOkay(requestResponse):
        if(len(requestResponse.text) > 0):
            return True
        else:
            return False
    @staticmethod
    def getDataFromJson(requestResponse):
        if checkRequestIsOkay(requestResponse):
            return json.loads(requestResponse.text)
        else:
            return {}

Upvotes: 0

Views: 228

Answers (1)

Phantoms
Phantoms

Reputation: 143

Don't use self. You need to use class name.

@staticmethod
def getDataFromJson(requestResponse):
    if Utils.checkRequestIsOkay(requestResponse):
        return json.loads(requestResponse.text)
    else:
        return {}

Upvotes: 1

Related Questions