Entry
How do I pass an integer variable (like a class attribute) by reference?
Jan 14th, 2004 19:38
Rene Aguirre, Khurram Ijaz,
Better use properties for this.
class a:
def __init__(self):
self._c=1
def setC(self,a):
self._c=a
def getC(self):
return self._c
c = property(getC,setC)
d = a()
d.c=2
print d.c
=== Rene Aguirre response ===
Actually I made this question when I was a Python beginner, my request
was simply to something like this:
def MyReferenceArgsFunction(refToInt, theObject):
# just use a convention to simplify, if you want to use a function
# like this in a class only, 'theObject' maybe is not needed
# (it would be 'self'), for an imported module just use the
# module name, "" for globals. Warning: No error checking!.
if theObject == "":
#convention: a global variable (on current global scope)
value = (globals())[refToInt] #retreive the value from ref.
else:
value = getattr(theObject, refToInt) #from object
#DO WHAT EVER YOU WANT WITH THE VALUE NOW
value = 1234 #this is why is reference to Int, use your favorite
#ok, now I'm setting the reference to the new value
if theObject == "":
(globals())[refToInt] = value
else:
setattr(theObject, refToInt, value)
#that's it! =======
Hope it helps
Rene