473,513 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behavior when printing a returned closure function

Hello,

when I execute the following code (python 2.5)

def f(x):
def g():
return x
return g

print f(1)
print f(2)

I get an output like

<function g at 0x00AFC1F0>
<function g at 0x00AFC1F0>

So according to print I get the same function object returned at both
calls.
That's surprising, I would expect to get two distinct function objects
because their func_closure attribute has to be different. And indeed,
if I do

print f(1) is f(2)

instead, it prints False. Even more confusing, if I do

g1 = f(1)
g2 = f(2)
print g1
print g2

I get something like

<function g at 0x00AFC1B0>
<function g at 0x00AFC1F0>

ie. two distinct function objects are printed.

What's happening here?
Some clever optimization reusing function objects in special cases or
what ...?

Thomas

Mar 25 '07 #1
2 1067
On Mar 25, 1:04 pm, Jean-Paul Calderone <exar...@divmod.comwrote:
On 25 Mar 2007 03:59:52 -0700, dart...@dicad.de wrote:
Hello,
when I execute the following code (python 2.5)
def f(x):
def g():
return x
return g
print f(1)
print f(2)
I get an output like
<function g at 0x00AFC1F0>
<function g at 0x00AFC1F0>
So according to print I get the same function object returned at both
calls.
That's surprising, I would expect to get two distinct function objects
because their func_closure attribute has to be different. And indeed,
if I do
print f(1) is f(2)
instead, it prints False. Even more confusing, if I do
g1 = f(1)
g2 = f(2)
print g1
print g2
I get something like
<function g at 0x00AFC1B0>
<function g at 0x00AFC1F0>
ie. two distinct function objects are printed.
What's happening here?
Some clever optimization reusing function objects in special cases or
what ...?

They're _not_ the same function object, just like the `is' test told you.
They just happen to have been allocated at the same memory address.

Jean-Paul
ah yes, I see, thanks

Mar 25 '07 #2
On Sun, 25 Mar 2007 03:59:52 -0700, dartsch wrote:

I get an output like

<function g at 0x00AFC1F0>
<function g at 0x00AFC1F0>

So according to print I get the same function object returned at both
calls.
Not the same function object. The first object is printed, then deleted
by the garbage collector because it goes out of scope. Then the second one
is created and just happens to end up in the same memory location. That's
an accident of the garbage collector implementation.

That's surprising, I would expect to get two distinct function objects
because their func_closure attribute has to be different. And indeed,
if I do
[snip]
<function g at 0x00AFC1B0>
<function g at 0x00AFC1F0>

ie. two distinct function objects are printed.
This time the first function still exists when the second is created, so
the second naturally can't be in the same memory location.

--
Steven.

Mar 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
1870
by: Simon Wittber | last post by:
For the first time, I have been bitten by Python. The below code produces the results: False True when I initially expected the results: False False It took me a while to work out that...
8
1700
by: Rahul | last post by:
Consider the following: def a(x): return x+1 def b(f): def g(*args,**kwargs): for arg in args: print arg return f(*args,**kwargs) return g
7
1697
by: Ziggy | last post by:
Just for curiosity. I know that main function is returning an int, but I came cross with the following code. (Unnessecary code is snipped) Is there any problem with it? #include <stdlib.h>...
3
1192
by: 63q2o4i02 | last post by:
Hi, I was wondering how I may get a python function to know what its name is without me having to write it manually? For example: def func1(): <do some stuff1> print 'func1' return True ...
23
1576
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
11
3007
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
6
1798
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
2
1576
by: KDawg44 | last post by:
Hi, I have the following code: $resultSet = mysql_query($sqlString); $numResults = mysql_num_rows($resultSet); echo "Num Results = " . $numResults . "<br/>"; for ($i = 0; $i < $numResults;...
160
5737
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
7257
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
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
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
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.