5
 >>> def foo(a):
        print "called the function"
        if(a==1):
            return 1
        else:
            return None
>>> a=1

>>> if(foo(a) != None and foo(a) ==1):
    print "asdf"

called the function
called the function
asdf

Hi. how can i avoid calling the function twice without using an extra variable.

2
  • 1
    you can save the result of foo(a) into a variable. Then check the variable value in if() clause. Commented Mar 14, 2013 at 11:25
  • 1
    Exactly, for the second condition to be True, the first has to be. Also note that brackets around the condition are considered bad form in Python. if(a==1): should just be if a==1:. Commented Mar 14, 2013 at 11:26

4 Answers 4

12

You can chain the comparisons like this

if None != foo(a) == 1:

This works like

if (None != foo(a)) and (foo(a) == 1):

except that it only evaluates foo(a) once.

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

6 Comments

You can chain equality comparisons? Well, look at that!
While this is a good general answer, the first check is still redundant in the given case.
@Lattyware, I assume the given case was just a poor example. NPE has that covered anyway
@gnibbler The OP's comment on NPE's answer says that 'my actual problem is similar to this'. And yeah, I'm just making the point that often logical wrangling can be avoided by simplifying the logic beforehand.
i gave poor example but was actually looking for this solution. i didn't knew we can chain that way.
|
7

how can i avoid calling the function twice without using an extra variable.

Here, you can simply replace

if(foo(a) != None and foo(a) ==1):

with

if foo(a) == 1:

The foo(a) != None is redundant: if foo(a) == 1, it is guaranteed to not be None.

6 Comments

Maybe the posted code is not the real code but just an example. if(foo(a) != None and foo(a) ==1): sounds as a better solution
@jimifiki If the function has side effects, that could cause problems. Besides, this is the best answer to the asked question. If the OP has over-simplified it, then he needs to ask a new question.
This is not answering the question. I agree with @jimifiki. And no it's not the best, see below
my bad i didn't knew that. this is the answer i need . my actual problem is similar to this. this works for me.
@uʍopǝpısdn It answers the question - this in no way deserves a -1, as it is correct. It might not be as general a solution, but it's the best solution for the given problem.
|
1

The following statement

if foo(a) == 1:

will deal with both conditions.

Comments

1

if foo(a)==1 ,then foo(a) will not be None,

so simplify your code to:

if foo(a):
    print('asdf')

2 Comments

Assuming that he means "Any value that evaluates to true" is a bit dodgy, I think.
i don't think there is a difference between if foo(a) and if foo(a)== 1 ,here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.