See exactly what a function has returned

B

Brad Tilley

Is there an easier way to do this:

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

Peter Hansen

Brad said:
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
 
B

Brad Tilley

Peter said:
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()))
 
J

Josh Close

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

Roy Smith

Brad Tilley said:
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?
 
P

Peter Grayson

def print_whats_returned(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
<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)
<type 'NoneType'>

Hope this helps.

Pete
 
B

Brad Tilley

Roy said:
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.
 
B

Brad Tilley

Peter said:
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



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



<type 'NoneType'>

Hope this helps.

Pete

Yes, it was very helpful. Thanks Pete.
 
J

Jeff Shannon

Brad said:
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
 
B

Brad Tilley

Jeff said:
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
 
D

Dennis Lee Bieber

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)



--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top