473,382 Members | 1,710 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,382 software developers and data experts.

storing references instead of copies in a dictionary

mk
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?

Jul 17 '08 #1
3 1473
On 17 Jul., 13:45, mk <mrk...@gmail.comwrote:
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


Jul 17 '08 #2
On Jul 17, 9:45 pm, mk <mrk...@gmail.comwrote:
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.
Jul 17 '08 #3
mk
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.

Jul 17 '08 #4

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

Similar topics

4
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,...
6
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...
3
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...
4
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...
22
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.