Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Return Statement

Reply
Thread Tools

Return Statement

 
 
sl33k_
Guest
Posts: n/a
 
      01-26-2011
How does "return True" and "return False" affect the execution of the
calling function?
 
Reply With Quote
 
 
 
 
rantingrick
Guest
Posts: n/a
 
      01-26-2011
On Jan 26, 2:26*pm, sl33k_ <(E-Mail Removed)> wrote:
> How does "return True" and "return False" affect the execution of the
> calling function?


>>> def f1():

pass

>>> print f1()

None


>>> def f2():

return

>>> print f2()

None


>>> def f3():

return True

>>> print f3()

True


>>> def f4():

return False

>>> print f4()

False


>>> def f5():

return 'Strawman'

>>> print f5()

Strawman


....any questions?
 
Reply With Quote
 
 
 
 
Stephen Hansen
Guest
Posts: n/a
 
      01-26-2011
On 1/26/11 12:26 PM, sl33k_ wrote:
> 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+4m hH6ktzIJlV7/wy
7C8PxpNwhGj8orj6QaMPt2Ex4biwBcoWqUT/A3uPCzhgvFtscX94LGOUwxKrKU3g
N7jxV9eSt1o9NUUai2Ol7vgk3hYc4iK0yHvjFEnAoaX0x+owTm 7YaQTs4Y8K0GSk
3zFuD1v6Ci+SCLu0NnOeWwCQgqdOmC9+qROHxaepl8BSA23APt N2lUYbJKQlaSOP
7Nl+vLonvQiRr14+AtYSCq2hptTu8U9iy7UX930bQp5DeqcTy+ rN3GWWLYkrI2Q=
=9ict
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Emile van Sebille
Guest
Posts: n/a
 
      01-26-2011
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

 
Reply With Quote
 
Alexander Kapps
Guest
Posts: n/a
 
      01-26-2011
On 26.01.2011 21:26, sl33k_ wrote:
> 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"
 
Reply With Quote
 
mpnordland
Guest
Posts: n/a
 
      01-26-2011
On 01/26/2011 03:26 PM, sl33k_ wrote:
> 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.

 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      01-27-2011
mpnordland wrote:
> On 01/26/2011 03:26 PM, sl33k_ wrote:
>> 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.


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
< if bool(statement): >, or, simplest,
< if statement: >

> 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~

 
Reply With Quote
 
mpnordland
Guest
Posts: n/a
 
      01-28-2011
I stand corrected

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM



Advertisments