How do I declare global vars or class vars in Python ?

Paddy O'Loughlin patrick.oloughlin at gmail.com
Tue Feb 17 10:10:51 EST 2009


Use the global statement.
http://docs.python.org/reference/simple_stmts.html#the-global-statement

A working example based on your pseudocode would be:
####################################
def getA():
    global A
    return A

def getB():
    global B
    return B

def setA(value):
    global A
    A = value

def setB(value):
    global B
    B = value

def main():
    setA(5)
    setB(6)
    print getA()
    print getB()

if __name__ == '__main__':
    main()

#####################################

Although honestly, I think you'd be best not coding like that (with
gets and sets and all) in python.

Paddy

2009/2/17 Linuxguy123 <linuxguy123 at gmail.com>:
> How do I do this in Python ?
>
> #############################
> declare A,B
>
> function getA
>    return A
>
> function getB
>    return B
>
> function setA(value)
>     A = value
>
> function setB(value)
>     B = value
>
> main()
>    getA
>    getB
>    dosomething
>    setA(aValue)
>    setB(aValue)
> ############################
>
> The part I don't know to do is declare the variables, either as globals
> or as vars in a class.  How is this done in Python without setting them
> to a value ?
>
> Thanks
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
"Ray, when someone asks you if you're a god, you say YES!"



More information about the Python-list mailing list