[Tutor] Re: Returned values

Ryan Sheehy the_investor at arach.net.au
Thu Dec 18 21:56:47 EST 2003


Thanks Lee and Andrei.

I have done some basic programming in a software package where the help
files contained info for a function and would be written as such:

xyz(whatever_variable: integer); boolean

... where the 'whatever_variable' would need to be an integer and the
function 'xyz' would return a boolean result. And I thought this would be
the norm with any programming language! :o)

I couldn't see these features in Python help files and was wondering how I
would know what the result would be.

I'm glad it can be done through the ways mentioned by all who responded...
as I need to unwire my brain from my previous function encounter and at the
moment this is the only way I can relate and understand Python.

Thanks again,


Ryan



-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On
Behalf Of Andrei
Sent: Friday, 19 December 2003 10:46 AM
To: tutor at python.org
Subject: [Tutor] Re: Returned values


Ryan Sheehy wrote on Fri, 19 Dec 2003 10:05:51 +0800:

<snip>
> abc = 1
> if xyz(abc) == 1:
>  ...etc
>
> ... now if xyz's output was boolean or a string (or anything other than
what
> I may have been expecting - i.e. 1) what would happen?

You can always try this kind of things out in the interpreter, it's one of
the great strenghts of Python. If you do that, you'll find out that you can
compare anything to anything. That means that if xyz doesn't return the
integer 1, that "==" will evaluate to False (regardless of what type the
result of xyz is).
So your code as presented above would work just fine, regardless of the
returns of xyz.

You should also note that you can often do this:

if xyz(abc):
  #something

Then if xyz returns anything but 0, None, False, or empty
dictionaries/sequences, it will be interpreted as True and #something is
executed

> This then brings me back to my original question - how would I know what
the
> function returns so that I can create a proper conditional statement with
> it.

I don't think you need to know the type, based on the info you gave. If you
believe type matters in your case, explain what you need in more detail.
Note that it is possible to get type info very easily using type(), it's
just that in the overwhelming majority of the cases it really isn't
necessary despite what you might think if coming from a statically typed
language.

--
Yours,

Andrei

=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.551 / Virus Database: 343 - Release Date: 11/12/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.551 / Virus Database: 343 - Release Date: 11/12/2003




More information about the Tutor mailing list