471,073 Members | 1,361 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

See exactly what a function has returned

Is there an easier way to do this:

def print_whats_returned(function):
print function
print type(function)
Jul 18 '05 #1
10 1550
Brad Tilley wrote:
Is there an easier way to do this:

def print_whats_returned(function):
print function
print type(function)


What is "this"? The subject line and the name of
the function imply it is something to do with the
return values of a function, yet the function is
never called. While Python is sometimes called
executable pseudo-code, I think in this case you'd
be better off using plain English to describe what
your goal is.

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Brad Tilley wrote:
Is there an easier way to do this:

def print_whats_returned(function):
print function
print type(function)

What is "this"? The subject line and the name of
the function imply it is something to do with the
return values of a function, yet the function is
never called. While Python is sometimes called
executable pseudo-code, I think in this case you'd
be better off using plain English to describe what
your goal is.

-Peter


OK,

def print_whats_returned(function):
## A function that shows what another function has returned
## as well as the 'type' of the returned data.
print function
print type(function)

def send_net_params_to_admin(ip_param, port_param):
ip = get_server_ip()
port = get_server_port()
return ip, port

print_whats_returned(send_net_params_to_admin(get_ server_ip(),
get_server_port()))
Jul 18 '05 #3
On Wed, 15 Sep 2004 13:30:22 -0400, Brad Tilley <br********@usa.net> wrote:
Peter Hansen wrote:
Brad Tilley wrote:
Is there an easier way to do this:

def print_whats_returned(function):
print function
print type(function)

What is "this"? The subject line and the name of
the function imply it is something to do with the
return values of a function, yet the function is
never called. While Python is sometimes called
executable pseudo-code, I think in this case you'd
be better off using plain English to describe what
your goal is.

-Peter


OK,

def print_whats_returned(function):
## A function that shows what another function has returned
## as well as the 'type' of the returned data.
print function
print type(function)

def send_net_params_to_admin(ip_param, port_param):
ip = get_server_ip()
port = get_server_port()
return ip, port

print_whats_returned(send_net_params_to_admin(get_ server_ip(),
get_server_port()))


def send_net_params_to_admin(ip_param, port_param):
ip = get_server_ip()
port = get_server_port()
return ip, port

print send_net_params_to_damin(ip_param, port_param)
Jul 18 '05 #4
In article <ci**********@solaris.cc.vt.edu>,
Brad Tilley <br********@usa.net> wrote:
Is there an easier way to do this:

def print_whats_returned(function):
print function
print type(function)


In the general case, this is not possible. A function can return
different things at different times. Consider the following function:

def getSomething ():
if random.random () < 0.5:
return 42
else:
return "fourty-two"

so what type would you say this returns?
Jul 18 '05 #5
Roy Smith wrote:
In article <ci**********@solaris.cc.vt.edu>,
Brad Tilley <br********@usa.net> wrote:

Is there an easier way to do this:

def print_whats_returned(function):
print function
print type(function)

In the general case, this is not possible. A function can return
different things at different times. Consider the following function:

def getSomething ():
if random.random () < 0.5:
return 42
else:
return "fourty-two"

so what type would you say this returns?


Depends on what randon.random came up with. Either way, we'd know what
the function returned and what type it was... and that's all I want to know.
Jul 18 '05 #6
> > def print_whats_returned(function):
## A function that shows what another function has returned
## as well as the 'type' of the returned data.
print function
print type(function)
This function does not do what you are expecting; type(function) does
not return the type that function returns, it returns the type of the
object passed in.

def foo():
return 42
print foo <function foo at 0xf6f888ec> print type(foo) <type 'function'>

Because Python is a dynamically typed language, it is not possible for
the interpreter to know what type of object a function returns without
executing it. In fact, it is possible for a function to return
different kinds of things. For example:

def bar(x):
if x == 0:
return 42
elif x== 1:
return "Forty-two"
else:
return None

In a statically typed language, like C, we neccessarily know what type
of thing is returned and we know it at compile-time. In Python, the
type of the returned object is only bound at run-time. In practice,
this means that we usually have to "just know" what a function returns
by knowing the intended semantics of the function. That said, you
could write a function like this that tells you about the return type
of a function for a given invocation of that function ...

def characterize(f, *args):
r = f(*args)
print type(r)
characterize(bar, 0) <type 'int'> characterize(bar, 1) <type 'str'> characterize(bar, 2)

<type 'NoneType'>

Hope this helps.

Pete
Jul 18 '05 #7
Peter Grayson wrote:
This function does not do what you are expecting; type(function) does
not return the type that function returns, it returns the type of the
object passed in.

def foo():
return 42

print foo
<function foo at 0xf6f888ec>
print type(foo)
<type 'function'>

Because Python is a dynamically typed language, it is not possible for
the interpreter to know what type of object a function returns without
executing it. In fact, it is possible for a function to return
different kinds of things. For example:

def bar(x):
if x == 0:
return 42
elif x== 1:
return "Forty-two"
else:
return None

In a statically typed language, like C, we neccessarily know what type
of thing is returned and we know it at compile-time. In Python, the
type of the returned object is only bound at run-time. In practice,
this means that we usually have to "just know" what a function returns
by knowing the intended semantics of the function. That said, you
could write a function like this that tells you about the return type
of a function for a given invocation of that function ...

def characterize(f, *args):
r = f(*args)
print type(r)

characterize(bar, 0)
<type 'int'>
characterize(bar, 1)
<type 'str'>
characterize(bar, 2)


<type 'NoneType'>

Hope this helps.

Pete


Yes, it was very helpful. Thanks Pete.
Jul 18 '05 #8
Brad Tilley wrote:

def print_whats_returned(function):
## A function that shows what another function has returned
## as well as the 'type' of the returned data.
print function
print type(function)

def send_net_params_to_admin(ip_param, port_param):
ip = get_server_ip()
port = get_server_port()
return ip, port

print_whats_returned(send_net_params_to_admin(get_ server_ip(),
get_server_port()))

Note here that the argument to print_whats_returned() is *not* a
function; it is the value returned from a function. You're not passing
the function send_net_params_to_admin(); you're passing whatever is
returned from it. It is effectively equivalent to the following:

return_value = send_net_params_to_admin(get_server_ip(),
get_server_port())
print_whats_returned(return_value)

Thus, your function would be much more clear with different names, since
it really has nothing to do with functions.

def print_value_and_type(value):
print value
print type(value)

With these names, you wouldn't have had all of these people talking
about dynamic typing and such, because it would've been clear that
you're not attempting to find the intended return value of a theoretical
future function call, but rather trying to inspect the actual value
that's already been returned by a previously-executed function call.

And no, there isn't really an easier way to do this. :)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #9
Jeff Shannon wrote:
Brad Tilley wrote:

def print_whats_returned(function):
## A function that shows what another function has returned
## as well as the 'type' of the returned data.
print function
print type(function)

def send_net_params_to_admin(ip_param, port_param):
ip = get_server_ip()
port = get_server_port()
return ip, port

print_whats_returned(send_net_params_to_admin(get_ server_ip(),
get_server_port()))


Note here that the argument to print_whats_returned() is *not* a
function; it is the value returned from a function. You're not passing
the function send_net_params_to_admin(); you're passing whatever is
returned from it. It is effectively equivalent to the following:

return_value = send_net_params_to_admin(get_server_ip(),
get_server_port())
print_whats_returned(return_value)

Thus, your function would be much more clear with different names, since
it really has nothing to do with functions.

def print_value_and_type(value):
print value
print type(value)

With these names, you wouldn't have had all of these people talking
about dynamic typing and such, because it would've been clear that
you're not attempting to find the intended return value of a theoretical
future function call, but rather trying to inspect the actual value
that's already been returned by a previously-executed function call.
And no, there isn't really an easier way to do this. :)

Jeff Shannon
Technician/Programmer
Credit International


Thanks Jeff,

You said what I was trying to say better than I could say it. I'm not
crazy after all... just picked the wrong words. This made sense to me at
first, then everyone said, "You can't do that," and I was almost at the
point of believing them when you posted. ;)

Brad
Jul 18 '05 #10
On Wed, 15 Sep 2004 13:30:22 -0400, Brad Tilley <br********@usa.net>
declaimed the following in comp.lang.python:

def print_whats_returned(function):
## A function that shows what another function has returned
## as well as the 'type' of the returned data.
print function
print type(function)

def send_net_params_to_admin(ip_param, port_param):
Why are you passing parameters for IP and port? You never use
them!
ip = get_server_ip()
port = get_server_port()
return ip, port

print_whats_returned(send_net_params_to_admin(get_ server_ip(),
get_server_port()))
And here you are invoking get_server_ip and get_server_port,
passing the values to a function that throws them away, because IT is
calling the same functions.

You can throw away the whole send_net_params_to_admin function
since all it is really returning is the same stuff you passed in (after
the overhead of ignoring what you passed in and recomputing them)

Your entire example condenses down to:

ip_port = (get_server_ip(), get_server_port())
print ip_port
print type(ip_port)

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by laredotornado | last post: by
33 posts views Thread by Pushkar Pradhan | last post: by
8 posts views Thread by Ravindranath Gummadidala | last post: by
8 posts views Thread by Ian Mackenzie | last post: by
7 posts views Thread by The87Boy | last post: by
7 posts views Thread by Terry Olsen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.