storing references instead of copies in a dictionary

bruno.desthuilliers at bruno.desthuilliers at
Fri Jul 18 14:13:17 CEST 2008

On 17 juil, 15:56, mk <mrk... at> wrote:
> Calvin Spealman wrote:
> > 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 change it? In other words,
> > define them all at once, and then just d['1'] = new_f1. What is wrong
> > with that?
> Well, basically nothing except I need to remember I have to do that.
> Suppose one does that frequently in a program. It becomes tedious. I
> think I will define some helper function then:
>  >>> def helper(fundict, newfun):
> ...     fundict[newfun.func_name] = newfun
> ...
> _If_ there were some shorter and still "proper" way to do it, I'd use
> it.

You're halfway there.

from functools import partial

callbacks = {}
register_callback = partial(helper, callbacks)

def f1(arg):
    print "f1", arg


def f1(arg):
    print "new f1", arg


More information about the Python-list mailing list