473,809 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_nam e] = 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_cod e

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
10 1511
On Jul 18, 1:32*am, John Machin <sjmac...@lexic on.netwrote:
On Jul 18, 4:26*pm, castironpi <castiro...@gma il.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
1570
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 object (I think it is a class but displays as a dictionary). My question is what is the requirement for an object to be able to store it in a dictionary? I am sure there is documentaion somewehre but I couldn't find it. Regards, -Jalil
6
2583
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 "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
12
3958
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
1230
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 can loop through it and pass its values to a new array, and the class instances will be passed by copy and not by reference?
6
3049
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
3
2024
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, but it does provide a rowid function. So, I am storing a dictionary of <string, long>, string being the rowid and long being a sum of the hash code for each column in the row. With this dictionary, it is very easy to determine if a row is...
7
5141
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 per row, where the average length of the two combined CLOB fields in the table was 12 bytes (4 bytes standard deviation). So i hex edited the .lb file and found the problem, each clob value is taking 0x400 bytes i.e. 1024 bytes.
2
3293
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 if you explicitly pass a dictionary with **kwargs, it still copies the dictionary anyway. -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://ironfroggy-code.blogspot.com/
3
1497
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 .... .... return "f1" + arg ....
0
187
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...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10379
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,...
0
9199
agi2029
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...
1
7660
isladogs
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...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
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...

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.