By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

assignment to reference

P: n/a
If

a = 'apple'
b = 'banana'
c = 'cabbage'

How can I get something like:-

for i in 'abc':
r = eval(i)
if r == 'cabbage': r = 'coconut'

actually change the object referenced by r rather
than creating a new object temporarily referenced by it?

I've tried playing with eval and exec without the desired
effect.
Oct 26 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Loris Caren enlightened us with:
If

a = 'apple'
b = 'banana'
c = 'cabbage'

How can I get something like:-

for i in 'abc':
r = eval(i)
if r == 'cabbage': r = 'coconut'

actually change the object referenced by r rather
than creating a new object temporarily referenced by it?
Use:

x = {
'a': 'apple',
'b': 'banana',
'c': 'cabbage'
}

for key, value in x.iteritems():
if value == 'cabbage':
x[key] = 'coconut'

NOTE: I haven't tested this code
I've tried playing with eval and exec without the desired effect.


If you notice yourself using such functions, it's ususally a lot
better to simply stash your data in a dictionary, and use that
instead.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Oct 26 '05 #2

P: n/a
Loris Caren wrote:
a = 'apple'
b = 'banana'
c = 'cabbage'

How can I get something like:-

for i in 'abc':
r = eval(i)
if r == 'cabbage': r = 'coconut'

actually change the object referenced by r rather
than creating a new object temporarily referenced by it?


if you need a container, you should use a container object instead of
the local namespace. in this case, a dictionary is what you want:

stuff = {
'a': 'apple',
'b': 'banana',
'c': 'cabbage'
}

for i in "abc":
if stuff[i] == "cabbage":
stuff[i] = "coconut"

(tweak as necessary)

</F>

Oct 26 '05 #3

P: n/a
Loris Caren a écrit :
If

a = 'apple'
b = 'banana'
c = 'cabbage'

How can I get something like:-

for i in 'abc':
r = eval(i)
if r == 'cabbage': r = 'coconut'

actually change the object referenced by r rather
than creating a new object temporarily referenced by it?

I've tried playing with eval and exec without the desired
effect.


for obj in (a, b, c):
if obj == 'cabbage':
obj = 'coconut'

Oct 26 '05 #4

P: n/a
I apparently don't understand this question in the same way the way
others do. I think the question is about the mutability of strings.

To my understanding of your question, it is impossible, at least if the
referenced objects are strings or other objects of immutable type.

'cabbage' cannot be changed into 'coconut', though list('cabbage') can
be changed to list('coconut') in place.
x = list('cabbage')
id(x) 458640 y = list('coconut')
id(y) 458736 x[:] = y[:]
x ['c', 'o', 'c', 'o', 'n', 'u', 't'] id (x) 458640 "".join(x)

'coconut'

You would need to be doing a LOT of these for any of this to be worth
the bother, or else have some rather exotic application you aren't
telling us about.

If I am reading you right the eval() is a red herring. I agree with the
others that you don't want an eval() here and you're better off with a
dict, but I am not sure that answers your question.

mt

Oct 26 '05 #5

P: n/a
Bruno Desthuilliers enlightened us with:
for obj in (a, b, c):
if obj == 'cabbage':
obj = 'coconut'


Doesn't work on my Python:

Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
a = 'apple'
b = 'banana'
c = 'cabbage'
for obj in (a, b, c): .... if obj == 'cabbage':
.... obj = 'coconut'
.... c 'cabbage'


Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Oct 27 '05 #6

P: n/a
Thank you all for your replies. They have helped me understand
that immutable means just that! Blame my c heritage where a
pointer allows you to scribble over anything.
Oct 27 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.