[Tutor] Returned values

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Dec 19 19:41:31 EST 2003


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

You don't because some functions return different types depending
on what they are passed as arguments. This is one of the features
that makes dynamic languages like Python or Perl different from
static ones like Java or C++

Consider:

def double(aVal = 42):
   return aVal * 2

double() -> 42
double(123) -> 246
double(12.3) -> 24.6000000
double("foo") -> 'foofoo'
double([1,2,3]) -> [1,2,3,1,2,3]
double({1:2,3:4})  -> Oops error!, can't multiply dictionaries

So the return value can be any type that supports multiplication
and is passed as an argument.

You can test the type of the return value but usually you try
to write code that doesn't rely on the type.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list