[Tutor] Returned values

Daniel Ehrenberg littledanehren at yahoo.com
Fri Dec 19 21:43:07 EST 2003


--- Alan Gauld wrote:
> > >>> 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

Are you sure double() (with no arguments) would return
42? I got 84.

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



More information about the Tutor mailing list