[Tutor] Returned values

Daniel Ehrenberg littledanehren at yahoo.com
Thu Dec 18 20:46:40 EST 2003


Ryan Sheehy wrote:
> I am just wondering how you would know what a
> function returns when it is
> called.
> 
> As an example, if I have code like so:
> 
> 	>>> abc = 1
> 	>>> xyz(abc)
> 	result of 'xyz'
> 
> ... where 'xyz' is any function that can accept
> 'abc' (without errors), how
> will I know what type of value 'xyz' has returned
> (boolean? string? integer?
> float?), obviously I might be able to see what it
> has returned in IDLE, but
> what if I cannot (for whatever reason)?
> 
> I hope my silly question makes sense. :o)
> 
> 
> Ryan

Python is dynamically typed, so you usually shouldn't
have to worry about types. But in general, you can
tell the type of something just by what is returned by
IDLE. When you just type something in to be evaluated,
it really gives you repr(whatever_you_typed_in). The
repr() function gives enough information to
distinguish any thing from any other thing, so it will
always have enough information to determine its type.
Here are some distinguishing factors about different
types:

Ints: These numbers have no decimal point and no
letters after them
Floats: These have decimal points but no letters
Longs: These have an L after them.
Note that for almost anything, ints, floats, and longs
are interchangable
Strings: These have either single or double quotes
around them.
Lists: These have [brackets] around them
Dicts: these have {curly:brackets} around them
Functions: these start with <function
Classes: these start with <class
Objects: These are surrounded by <> and the second
word is instance
Modules: These start with <module
Files: Surrounded by <>, second word file
Builtins: start with <built-in
Bools: either True or False
NoneType: always None or just goes to next line
without printing anything.
Complex number: In form (1+2j)
Tuples: A few items seperated by commas and the whole
thing is surrounded by parenthases

Wow. I didn't realize Python had so many types until I
listed them. This isn't even all of them; do 'import
types' for more.

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Tutor mailing list