Hello everyone,
I'm storing functions in a dictionary (this is basically for cooking up
my own fancy schmancy callback scheme, mainly for learning purpose):
>>def f2(arg):
.... return "f2 " + arg
....
>>> def f1(arg):
.... return "f1" + arg
....
>>a={'1': f1, '2': f2}
[ x[1](x[0]) for x in a.items() ]
['f11', 'f2 2']
Well, neat. Except if I change function definitions now, old functions
are called. And rightly:
{'1': <function f1 at 0xb7f0ba04>, '2': <function f2 at 0xb7f0b9cc>}
>>f1
<function f1 at 0xb7f0ba04>
>>> def f1(arg):
.... return "NEW f1 " + arg
....
>>f1
<function f1 at 0xb7f0b994>
The address of function f1 has obviously changed on redefinition.
Storing value copies in a dictionary on assignment is a reasonable
default behaviour.
However, in this particular case I need to specifically store
_references to objects_ (e.g. f1 function), or should I say _labels_
(leading to objects)?
Of course, I can basically update the dictionary with a new function
definition.
But I wonder, is there not a way _in general_ to specifically store
references to functions/variables/first-class objects instead of copies
in a dictionary? 3 1493
On 17 Jul., 13:45, mk <mrk...@gmail.c omwrote:
Hello everyone,
I'm storing functions in a dictionary (this is basically for cooking up
my own fancy schmancy callback scheme, mainly for learning purpose):
*>>def f2(arg):
... * * return "f2 " + arg
...
*>>>
*>>def f1(arg):
... * * return "f1" + arg
...
*>>a={'1': f1, '2': f2}
*>>>
*>>[ x[1](x[0]) for x in a.items() ]
['f11', 'f2 2']
Well, neat. Except if I change function definitions now, old functions
are called. And rightly:
{'1': <function f1 at 0xb7f0ba04>, '2': <function f2 at 0xb7f0b9cc>}
*>>f1
<function f1 at 0xb7f0ba04>
*>>>
*>>def f1(arg):
... * * return "NEW f1 " + arg
...
*>>f1
<function f1 at 0xb7f0b994>
The address of function f1 has obviously changed on redefinition.
Storing value copies in a dictionary on assignment is a reasonable
default behaviour.
However, in this particular case I need to specifically store
_references to objects_ (e.g. f1 function), or should I say _labels_
(leading to objects)?
Of course, I can basically update the dictionary with a new function
definition.
But I wonder, is there not a way _in general_ to specifically store
references to functions/variables/first-class objects instead of copies
in a dictionary?
Python stores references in dictionaries and does not copy ! (unless
you explicitly use the copy module) !
In your case the entry in the dictionary is a reference to the same
object which f1 references, that is the object at 0xb7f0ba04.
If you now say "f1=...:" then f1 references a new object
at 0xb7f0b994, and the entry in your dictionary still references
the "old" object at 0xb7f0ba04.
I do not know any method to automatically update your dictionary
as there is no possibility to overload the assignement operator "=".
But may be somebody can teach me a new trick :-)
Greetings, Uwe
On Jul 17, 9:45 pm, mk <mrk...@gmail.c omwrote:
Hello everyone,
I'm storing functions in a dictionary (this is basically for cooking up
my own fancy schmancy callback scheme, mainly for learning purpose):
>>def f2(arg):
... return "f2 " + arg
...
>>>
>>def f1(arg):
... return "f1" + arg
...
>>a={'1': f1, '2': f2}
>>>
>>[ x[1](x[0]) for x in a.items() ]
['f11', 'f2 2']
Well, neat. Except if I change function definitions now, old functions
are called. And rightly:
{'1': <function f1 at 0xb7f0ba04>, '2': <function f2 at 0xb7f0b9cc>}
>>f1
<function f1 at 0xb7f0ba04>
>>>
>>def f1(arg):
... return "NEW f1 " + arg
...
>>f1
<function f1 at 0xb7f0b994>
The address of function f1 has obviously changed on redefinition.
I wouldn't put it like that. You have created a new function, with a
different address to the original function, and bound the name "f1" to
that new function. The address of the old function is still stored in
the dictionary.
A function (or any other object) can have 0, 1, or many names:
>>def foo():
.... print "The function formerly known as foo"
....
>>fred = foo # 2 names del foo # back to one name fred()
The function formerly known as foo
>>L = [fred] del fred # 0 names L[0]() # You can't keep a good function down ...
The function formerly known as foo
>>>
Of course, I can basically update the dictionary with a new function
definition.
Uh-huh ...
>
But I wonder, is there not a way _in general_ to specifically store
references to functions/variables/first-class objects instead of copies
in a dictionary?
Yup, and that's what happens all the time, unless you explicitly make
a copy ... some objects have a copy method, sequences can be copied by
taking a full slice (seq_copy = seq[:]), otherwise read up on the copy
module.
Uwe Schmitt wrote:
Python stores references in dictionaries and does not copy ! (unless
you explicitly use the copy module) !
In your case the entry in the dictionary is a reference to the same
object which f1 references, that is the object at 0xb7f0ba04.
If you now say "f1=...:" then f1 references a new object
at 0xb7f0b994, and the entry in your dictionary still references
the "old" object at 0xb7f0ba04.
Erm, I theoretically knews that, I guess I suffered temporary insanity
when I wrote "copies" of objects. To me it seems that Python actually
stores _labels_ referencing _objects_. Here, the problem was that the
label in the dictionary led to the "old" object.
I do not know any method to automatically update your dictionary
as there is no possibility to overload the assignement operator "=".
But may be somebody can teach me a new trick :-)
Theoretically I could define a class inheriting from dictionary and
define a property with a setter (and getter). But I would still have to
update the attribute manually, so plain dictionary is just as good. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thomas Philips |
last post by:
I want to represent an NxN matrix by a list containing N lists, each
of which has N elements. Initially the elements are set to " ". For
N=2, I write
>>>x = *2 #assignment creates references, not copies!
>>>x
>>>y = ]*2
>>>y
, ]
|
by: bissatch |
last post by:
Hi,
I am currently writing a news admin system. I would like to add the
ability to add images to each article.
What I have always done in the past is uploaded (using a form) the
image to a folder on the server and then in the database table that I
INSERT the news article, I'll store the path of the uploaded image.
To me this seems a bad idea as if the image paths were changed on the
|
by: mike.aes |
last post by:
(ASP.net 2.0)
I am using a Gridview control that is bound to a table in my SQL
database. For some reason, when the Checkbox is clicked, it stores a 1
(Positive 1) in the bound field instead of a -1 (Negative 1). Any
ideas what's going on?
|
by: 63q2o4i02 |
last post by:
Hi,
I'm writing a hand-written recursive decent parser for SPICE syntax
parsing. In one case I have one function that handles a bunch of
similar cases (you pass the name and the number of tokens you're
looking for). In another case I have a function that handles a
different set of tokens and so it can't use the same arguments as the
first one, and in fact takes no arguments. However, these functions
are semantically similar and are...
|
by: mehdi_mousavi |
last post by:
Hi folks,
Consider the following line of code:
Address addr = ei.Address;
where ei is an instance of a class that has got a property of Address
type.
The question is that what is happening in the above assignment? Unlike
the C/C++, it seems that the above code instructs the compiler to point
| |
by: Calvin Spealman |
last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote:
As was pointed out already, this is a basic misunderstanding of
assignment, which is common with people learning Python.
To your actual problem... Why do you wanna do this anyway? If you want
to change the function in the dictionary, why don't you simply define
the functions you'll want to use, and change the one you have bound to
the key in the dictionary when you want to...
|
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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |