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 2 1085
On Mar 25, 1:04 pm, Jean-Paul Calderone <exar...@divmod .comwrote:
On 25 Mar 2007 03:59:52 -0700, dart...@dicad.d e 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 default keyword argument values
|
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
|
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>
void main(){
exit(EXIT_SUCCESS);
}
|
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
def func2():
<do some stuff2>
|
by: g.ankush1 |
last post by:
#include <stdio.h>
/* 1st example
int a()
{
return 1;
}
| |
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()
|
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. One of the requirements is that the
calendar will need to display a varying number of months (1..3)
|
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; $i++) {
$row = mysql_fetch_array($resultSet);
echo "i = " . $i . "<br/>";
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |