storing references instead of copies in a dictionary

M

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):
.... return "f2 " + 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:

.... return "NEW f1 " + arg
....<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?
 
U

Uwe Schmitt

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
 
J

John Machin

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):
... return "f2 " + 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:

... return "NEW f1 " + arg
...<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:
.... 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.
 
M

mk

Uwe said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top