B
Brad Tilley
Is there an easier way to do this:
def print_whats_returned(function):
print function
print type(function)
def print_whats_returned(function):
print function
print type(function)
Brad said:Is there an easier way to do this:
def print_whats_returned(function):
print function
print type(function)
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()))
Brad Tilley said:Is there an easier way to do this:
def print_whats_returned(function):
print function
print type(function)
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?
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
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()))
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
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()))
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.