3

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
2
  • The whole point of functions is just that though Commented Jul 14, 2017 at 17:35
  • Thanks to everyone who answered below. I now know that I missed something very important about what the return statement does. I mistakenly thought that if both if statementa were True the first return statement would be replaced by the second. I feel really dumb now, but I'm also very happy that I now learned this very important point. It will really make coding much simpler for me now. So thank you to all of you. Commented Jul 14, 2017 at 17:46

6 Answers 6

5

You can use a second if:

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

Since the first if contains a return statement, the interpreter will never reach your elif line if the constraint is satisfied.

Sign up to request clarification or add additional context in comments.

Comments

4

Just use if instead of elif. The el[se] is redundant because of the return anyway.

Comments

2

Note that the return exits the function, so you don't have to worry about your branch logic. You're correct that you shouldn't call a function twice: continue to use temp for each call.

You have two choices. The first is to simply use a new if; the old one already left the function.

temp = self.get_data('text1', bsObj)
if temp > -1:
    return temp

temp = self.get_info('text', bsObj)
if temp > -1:
    return temp

return 0

The other choice is to correctly wrap the "else" logic in that block:

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

Comments

1

Try this:

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

So what I've done is switched your if and elif cases. You only want to change your temp definition if temp < -1, and if temp = -1. (really, even the pass is a little redundant, if you don't want to keep it you should be able to get rid of it just fine.)

Comments

1

Written in a more concise way:

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

Comments

1

I don't know if this works, but I decided to post this for fun.

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

2 Comments

Note that get_info is being called, regardless of the value of self.get_data('text1', bsObj), where in the original code, it does not
Yes I am aware of that. Efficiency though is relative to whether or not self.get_info() will be called often or not every time get_number() is called. We do not know the answer to that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.