Home     My Faqts     Contributors     About     Help    

faqts : Computers : Programming : Languages : Python

FAQTs repaired & updated!
Thanks for your patience...
Entry Add Entry Alert - Edit this Entry

Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes
Recently 2 of 3 people (67%) answered Yes

How do I pass an integer variable (like a class attribute) by reference?

Jan 15th, 2004 14: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



© 1999-2004 Synop Pty Ltd