473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1675
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
33
by: Pushkar Pradhan | last post by:
I'm using clock() to time parts of my code e.g. clk1 = clock(); /* code */ clk2 = clock(); /* calculate time in secs */ ...... clk1 = clock(); /* code */ clk2 = clock();
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
8
by: Ian Mackenzie | last post by:
Hi Guys I am VERY new to DB2 and have created a workingdays function to return the working days between 2 dates, but I get some compiler errors when running it: CREATE FUNCTION WORKINGDAYS...
7
by: The87Boy | last post by:
Is there any ways to make a function like system(), which returns more than one value (true or false) and (the array)
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
1
by: Paul Childs | last post by:
Hi folks, I'll start off with the code I wrote... (ActivePython 2.4 on Windows XP SP2) ------------------------------- class FlightCondition(object): lsf = vto =
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.