[Tutor] inquire

D-Man dsh8290@rit.edu
Thu, 11 Jan 2001 12:10:21 -0500


On Thu, Jan 11, 2001 at 10:50:46AM -0500, Michael P. Reilly wrote:
| > 
| > --=====_97922590441=_
| > Content-Type: text/plain; charset="us-ascii"
| > 
| > Dear Sir/Madam,
| > 
| > Does python offer the function like the "&" in FoxPro? 
| > i.e. in foxpro, some code like following:
| > 
| >             a="I am a"
| >             b="a"
| >             print &b
| > 
| > may print the content of variable a.
| > Can one do like this in python?
| > 
| > Many Thanks,
| > Xia XQ
| > 
| 

I've never used FoxPro, but I'll try to help anyways.

| You're thinking of "soft references".  You can do that in Python, but
| not in the same way; everything is a reference after all.  Because of
| the namespace rules in Python, it is easier to use eval (or exec) to do
| what you want.
| 
| >>> a = "I am a"
| >>> b = "a"
| >>> print eval(b)
| I am a

Couldn't you just do:

a = "I am a"
b = a
print b

?

In this case, changing the value of a won't affect b.  To have this
effect (without using eval as already demonstrated) you would need 2
layers of references -- a reference to the string, then a & b would be
references to that reference.  Then you would modify the external
reference to make sure a & b refer to the new value.

ex:

class Pointer : 
	def __init__( self , value ) :
		self.value = value

c = Pointer( "The value" )
a = c
b = c
print a.value  # The value
print b.value  # The value
c.value = "A new value"
print a.value  # A new value
print b.value  # A new value


In this case, c is an object that has a member called "value".  When
the assignment c.value = "A new value" is executed, the value of
'value' changes, but the value of 'c' doesn't.  a and b are references
to c.  Since c didn't change, a and b refer to the same object, whose
member changed.


Does this help at all?

-D


PS.  There is probably an easier way to have "pointers" in python, but
someone more experienced will have to demonstrate this.