0

I have two functions with one containing a return statement. My agenda is when the first function executes in the if condition, compiler is throwing a syntax error.

Here's my script.

def hi():
  return 10,20

def hello(a):
  if a:
    print "asd"

if a,b=hi() : hello(a)

The error is thrown at "=" in the if condition. How to solve this. Or is there any alternative to perform this in one liner?

6
  • 2
    Use = for value assignment, but == for value comparison. Commented Jul 1, 2019 at 10:58
  • And what are a and b Commented Jul 1, 2019 at 11:00
  • Notice that every function has return type. Even if there is no return in function, it has return None (or just return) behind the scenes. Commented Jul 1, 2019 at 11:01
  • The meaning of "this" at the bottom of your question is not clear. What exactly are you trying to accomplish? Commented Jul 1, 2019 at 11:05
  • Possible duplicate of stackoverflow.com/questions/2603956/… Commented Jul 1, 2019 at 11:10

2 Answers 2

3

You need to create a tuple of a and b then you can compare using ==

Use this,

if (a,b)==hi() : hello(a)

Here is the full code which prints asd

def hi():
  return 10,20

def hello(a):
  if a:
    print "asd"

a,b = 10,20

if (a,b)==hi() : hello(a)
Sign up to request clarification or add additional context in comments.

3 Comments

But what if the return values inside the function hi() is dynamic?.
so should I use a global tuple to have a dynamic value?
I don't think you need global tuple. If the return value inside hi() function is, let's say return x,y where x and y are variables which you can set before, your if condition will simply check the actual value tuples, if they are equal or not. For example, if a,b=10,20 and x,y=5,10 then your if will evaluate to look like if (10,20)==(5,10) which will be false and hello(a) won't be executed.
0

You can't have arbitrary expressions in an if statement, you can only use a boolean like value (something that can be interpreted as true/false).

So you need to do something like this

def hi():
  return 10,20

def hello(a):
  if a:
    print "asd"

a, b = hi() 
if a:
    hello(a)

Comments

Your Answer

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