vendredi 14 juillet 2017

Python variable between if and elif

The following is invalid syntax as the elif needs to be right after the if block:

def get_number(self, bsObj):
    temp = self.get_data('text1', bsObj)
    if temp > -1:
        return temp
    temp = self.get_info('text', bsObj)
    elif temp > -1:
        return temp
    else:
        return 0

I only want to return the result of self.get_info('text', bsObj) if self.get_data('text1', bsObj) is less than -1.

I could fix it by doing this, but it seems very ineffective to call the same function twice:

def get_number(self, bsObj):
    if self.get_data('text1', bsObj) > -1:
        return self.get_data('text1', bsObj)
    elif self.get_info('text', bsObj) > -1:
        return self.get_data('text', bsObj)
    else:
        return 0

Aucun commentaire:

Enregistrer un commentaire