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

Re: storing references instead of copies in a dictionary

mk
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. If not, no big deal.
For completeness:

def new_f1(arg):
return "NEW f1 " + arg
f1.func_code = new_f1.func_code

Don't use that unless you really have to and I nearly promise that you don't.
I promise I won't use it. :-) It seems like a 'wrong thing to do'.
Jul 17 '08 #1
10 1463
mk 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. If not, no big deal.
>For completeness:

def new_f1(arg):
return "NEW f1 " + arg
f1.func_code = new_f1.func_code

Don't use that unless you really have to and I nearly promise that you
don't.

I promise I won't use it. :-) It seems like a 'wrong thing to do'.

Well it's probably totally "non pythonic" but this does what you want:

def f2(arg):
return "f2 "+arg

def f1(arg):
return "f1 "+arg

a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]

Don't know if this is any use to you..

Jul 17 '08 #2
mk
def f2(arg):
return "f2 "+arg

def f1(arg):
return "f1 "+arg

a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.
Don't know if this is any use to you..
At least I learned something. :-)


Jul 17 '08 #3
On Jul 17, 10:05*am, mk <mrk...@gmail.comwrote:
def f2(arg):
* * return "f2 "+arg
def f1(arg):
* * return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
* * return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]

Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.
Don't know if this is any use to you..

At least I learned something. :-)
You want consistent access to a changing variable. Wrap it in an
object:
>>a= Blank( )
a.ref= 'X'
a.ref
'X'
>>b= a
b.ref
'X'
>>a.ref= 'Y'
b.ref
'Y'
>>>
Jul 17 '08 #4
castironpi wrote:
On Jul 17, 10:05 am, mk <mrk...@gmail.comwrote:
>>def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.
>>Don't know if this is any use to you..
At least I learned something. :-)

You want consistent access to a changing variable. Wrap it in an
object:
>>>a= Blank( )
a.ref= 'X'
a.ref
'X'
>>>b= a
b.ref
'X'
>>>a.ref= 'Y'
b.ref
'Y'
My "old fashioned" programing paradigms think of this in terms of
"pointers", a throw back to my schooling in 'C'. I find this general
form of problem to be common across languages and in some ways hard to
express in python. The whole idea of labels bound to objects is quite
alien to traditional terminology. I find one of the main attractions of
python is this new mindset that the language makes you adopt - a
different set of tools are at hand for the old school programmer.

castironpi - please give an example of what you are thinking as I find
this interesting. preferably post some brief example code.
Jul 18 '08 #5
bgeddy wrote:
castironpi wrote:
>On Jul 17, 10:05 am, mk <mrk...@gmail.comwrote:
>>>def f2(arg):
return "f2 "+arg
def f1(arg):
return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.

Don't know if this is any use to you..
At least I learned something. :-)

You want consistent access to a changing variable. Wrap it in an
object:
>>>>a= Blank( )
a.ref= 'X'
a.ref
'X'
>>>>b= a
b.ref
'X'
>>>>a.ref= 'Y'
b.ref
'Y'
My "old fashioned" programing paradigms think of this in terms of
"pointers", a throw back to my schooling in 'C'. I find this general
form of problem to be common across languages and in some ways hard to
express in python. The whole idea of labels bound to objects is quite
alien to traditional terminology. I find one of the main attractions of
python is this new mindset that the language makes you adopt - a
different set of tools are at hand for the old school programmer.

castironpi - please give an example of what you are thinking as I find
this interesting. preferably post some brief example code.
castironpi - please forgive the double post but my newsreader didn't
display your code correctly.. Doh !! Anyway - a nice way of addressing
the problem. However the OP's post revolved around having a rewritable
set of "labels" - which could be recorded at one time and when re
referenced the new definitions of those labels would be used. For
example a "collection" (list,dictionary,tuple) could be made of these
"labels" and then the underlying code accessed by the labels changed. If
the code was now ran indirectly by referencing the list then the new
code would be ran. These building blocks are how parsers are built and
the basis of language.
I can see how you form two ways of addressing the variable but can't
figure how this fits the original problem. Please elaborate for my
ignorance.

EdH.
Jul 18 '08 #6
On Jul 18, 10:36 am, bgeddy <bge...@home.havin.a.breakwrote:
[snip]
the OP's post revolved around having a rewritable
set of "labels" - which could be recorded at one time and when re
referenced the new definitions of those labels would be used. For
example a "collection" (list,dictionary,tuple) could be made of these
"labels" and then the underlying code accessed by the labels changed. If
the code was now ran indirectly by referencing the list then the new
code would be ran.
This notion is as dangerous and ludicrous as the COBOL ALTER verb.
These building blocks are how parsers are built and
the basis of language.
Huh?
Jul 18 '08 #7
On Jul 17, 7:36*pm, bgeddy <bge...@home.havin.a.breakwrote:
bgeddy wrote:
castironpi wrote:
On Jul 17, 10:05 am, mk <mrk...@gmail.comwrote:
def f2(arg):
* * return "f2 "+arg
def f1(arg):
* * return "f1 "+arg
a={"1":"f1","2":"f2"}
print [eval(x[1])(x[0]) for x in a.items()]
def f2(arg):
* * return "New f2 "+arg
print [eval(x[1])(x[0]) for x in a.items()]
Neat trick, if probably dangerous in some circumstances. Anyway, thanks,
I didn't think of that.
>>Don't know if this is any use to you..
At least I learned something. :-)
You want consistent access to a changing variable. *Wrap it in an
object:
>>>a= Blank( )
a.ref= 'X'
a.ref
'X'
b= a
b.ref
'X'
a.ref= 'Y'
b.ref
'Y'
My "old fashioned" programing paradigms think of this in terms of
"pointers", a throw back to my schooling in 'C'. I find this general
form of problem to be common across languages and in some ways hard to
express in python. The whole idea of labels bound to objects is quite
alien to traditional terminology. I find one of the main attractions of
python is this new mindset that the language makes you adopt - a
different set of tools are at hand for the old school programmer.
castironpi - please give an example of what you are thinking as I find
this interesting. preferably post some brief example code.

castironpi *- please forgive the double post but my newsreader didn't
display your code correctly.. Doh !! Anyway - a nice way of addressing
the problem. However the OP's post revolved around having a rewritable
set of "labels" - which could be recorded at one time and when re
referenced the new definitions of those labels would be used. For
example a "collection" (list,dictionary,tuple) could be made of these
"labels" and then the underlying code accessed by the labels changed. If
the code was now ran indirectly by referencing the list then the new
code would be ran. These building blocks are how parsers are built and
the basis of language.
I can see how you form two ways of addressing the variable but can't
figure how this fits the original problem. Please elaborate for my
ignorance.

EdH.
In the OP's post, we have:

def f1(): print 'f1'
def f2(): print 'f2'
funs= [ f1, f2 ]
def f1(): print 'new f1'

They wanted funs[ 0 ] to contain this new function / new definition
too.

Another language might permit:
def funs[ 0 ](): print 'new f1'

Python allows:

def f1(): print 'new f1'
funs[ 0 ]= f1

and

@rebind( funs, 0 )
def f1(): print 'new f1'

and

@rebind_named( funs, 'funA' )
def f1(): print 'new f1'

in the case funs was a dictionary or class or class instance.
Functions remain to be first class objects; their definition syntax is
merely restricted over normal assignments.

To access "whatever 'f1' is pointing to right now", the only way is
with eval, which you showed.

Spealman's solution can accomplish redefinition, but only provided
calling signature and closure, among others, don't change. If they
do, you have to reassign those, too, and possibly more:
>>dir( f )
[snip]
'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc',
'func_globals', 'func_name'

Roughly the same in 3.0. It's an option.
These building blocks are how parsers are built and
the basis of language.
To address this, I observe altered references are easy for computers.
Suppose:

#--NOT PYTHON--
def f1(): print 'f1'
008902 f1: 0x008934
def f1(): print 'new f1'
008902 f1: 0x008938

'f1()' calls the function the address of which is stored at 008902 in
each case: f1 (at 008934) in the first case, and new f1 (at 008938) in
the second.

Python eliminates that combination from the syntax, which strengthens
object-name identity, at the cost of a slightly longer workaround
(a.ref= f1) for keeping names but changing objects. In this sense,
the only 'pointer' per se is the string name of an object, scoped by a
namespace-- eval( 'f1', spaceA ).

I delicately ask for an example in natural language and daily life in
which we change what object a name refers to, or hold that Python
conforms to natural language better than computer-native mutable
pointers and references.
Jul 18 '08 #8
On Jul 18, 4:26*pm, castironpi <castiro...@gmail.comwrote:
I delicately ask for an example in natural language and daily life in
which we change what object a name refers to,
her, him, it, ... i.e. any pronoun
Jul 18 '08 #9
On 17 juil, 15:56, mk <mrk...@gmail.comwrote:
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)

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

callbacks['f1']('ok')

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

callbacks['f1']('ok')

Jul 18 '08 #10
On Jul 18, 1:32*am, John Machin <sjmac...@lexicon.netwrote:
On Jul 18, 4:26*pm, castironpi <castiro...@gmail.comwrote:
I delicately ask for an example in natural language and daily life in
which we change what object a name refers to,

her, him, it, ... i.e. any pronoun
In that case,

it= Dog( )
refs[ 'it' ]= it
it.walk( )
it= Cat( )
it.feed( )

you don't want refs[ 'it' ] to refer to cat. You were talking about
the dog when you stored refs[ 'it' ].

The OP might be wanting to mutate locals( ). If not define a
Reference object,

class Reference:
def __init__( self, ref ): self.ref= ref

a more descriptive name than Blank (above), to explicitly state that
you're using a reference instead of "fixed-bound" namespace semantics,
or whatever the right word is.
Jul 18 '08 #11

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

Similar topics

0
by: Feghhi, Jalil | last post by:
I get an error storing an object in a dictionary. The dictionary and the object are both provided by mod_python. One is the session object (which is just a dictionary) and one is a FieldStorage...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
5
by: lkrubner | last post by:
Is it true that Javascript has no clone() method, to pass an object by copy of value instead of reference? If I have an object and want to make an array out of all of its instance variables, I...
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: Brian | last post by:
I know this is the wrong way to do it, but maybe someone can tell me the right way to do it... I have two different databases that I need to synchronize. The database doesn't have keys exactly,...
7
by: Ian Boyd | last post by:
Customer wanted us to figure out why their database was growing at an astronomical rate. We broke down the various fields making up a table, and we found that the ..LB file was using about 1k...
2
by: Calvin Spealman | last post by:
In the internal API when a C function is called and passed a kwarg dictionary, is there any case where anything else has a reference to it? I checked with the following code and it looks like even...
3
by: mk | last post by:
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 .......
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.