Return Statement

S

sl33k_

How does "return True" and "return False" affect the execution of the
calling function?
 
S

Stephen Hansen

How does "return True" and "return False" affect the execution of the
calling function?

It doesn't -- the value 'True' or 'False' is simply returned, and
assigned to a name if the calling function does so explicitly. But
there's no built in affects. If you want it to alter the execution, you
have to do so yourself, i.e.:

def myfun():
return True
def myfun2():
return False

if myfun():
print "Something is true!"

myfun2()
print "I'm called. Cuz, the return value of myfun2 was simply discarded."

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJNQJMcAAoJEKcbwptVWx/lVDUH/3TGyob9QXqjWhr2NWUouB6+
hcKlnUVv4yUq99cRQd6oFFwhnN7l7UvAiLibs7TwwV1Llar+4mhH6ktzIJlV7/wy
7C8PxpNwhGj8orj6QaMPt2Ex4biwBcoWqUT/A3uPCzhgvFtscX94LGOUwxKrKU3g
N7jxV9eSt1o9NUUai2Ol7vgk3hYc4iK0yHvjFEnAoaX0x+owTm7YaQTs4Y8K0GSk
3zFuD1v6Ci+SCLu0NnOeWwCQgqdOmC9+qROHxaepl8BSA23APtN2lUYbJKQlaSOP
7Nl+vLonvQiRr14+AtYSCq2hptTu8U9iy7UX930bQp5DeqcTy+rN3GWWLYkrI2Q=
=9ict
-----END PGP SIGNATURE-----
 
E

Emile van Sebille

On 1/26/2011 12:26 PM sl33k_ said...
How does "return True" and "return False" affect the execution of the
calling function?

That depends on the calling function. It will control what it does next
generally based on the returned value, but it could also simply store
the result.

def isACustomer(custID):
1 return custID in currentCustList

def isActive(custId):
2 return custID in openInvoicesByCustID

for custID in custList:
3 if isACustomer(custID):
4 activeFlag = isActive(custID)


Here, 1 and 2 return True or False depending on the inclusion test.

3 causes the subsequent code block to be executed when True is returned

4 stores True of False in activeFlag for subsequent use.

Emile
 
A

Alexander Kapps

How does "return True" and "return False" affect the execution of the
calling function?

If only affects the calling function if you use the return value:

def foo():
return True

def bar1():
foo() # nothing difference, whether foo() returns True or False

def bar2()
if foo():
print "foo returned True or any other non-false value"
else:
print "foo returned False or any other non-True value"
 
M

mpnordland

How does "return True" and "return False" affect the execution of the
calling function?

Basically it will affect it in whatever way you design it to for example:
def lie_test(statement):
if statement is True:
return False
else:
return False
Now, this is psuedo code somewhat.
"if statement is True:" would always equate to "True" unless statement
was an empty string, None, or 0. As to return False if statement equals
true, look at the function name. It is testing to see if it is a lie,
and if it is true, then it's not a lie.
 
E

Ethan Furman

mpnordland said:
Basically it will affect it in whatever way you design it to for example:
def lie_test(statement):
if statement is True:
return False
else:
return False
Now, this is psuedo code somewhat.
"if statement is True:" would always equate to "True" unless statement
was an empty string, None, or 0.

Um, no.

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
--> if 'some stuff' is True:
.... print 'True'
.... else:
.... print "it's a dang lie!"
....
it's a dang lie!

You need either < if bool(statement) is True: >, or
or said:
As to return False if statement equals
true, look at the function name. It is testing to see if it is a lie,
and if it is true, then it's not a lie.

Your last statement, though, should be return True -- the way you have
it now the function always returns False.

~Ethan~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top