[Tutor] Re: Returned values

Andrei project5 at redrival.net
Thu Dec 18 21:45:56 EST 2003


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.




More information about the Tutor mailing list