assignment to reference

L

Loris Caren

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.
 
S

Sybren Stuvel

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
 
F

Fredrik Lundh

Loris said:
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 == "cabbage":
stuff = "coconut"

(tweak as necessary)

</F>
 
B

Bruno Desthuilliers

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'
 
M

Michael Tobis

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
 
S

Sybren Stuvel

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..... if obj == 'cabbage':
.... obj = 'coconut'
....
Sybren
 
L

Loris Caren

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.
 

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
474,269
Messages
2,571,338
Members
48,025
Latest member
Rigor4

Latest Threads

Top