Variable scope within a class

Graham gsmith at oxfam.org.uk
Tue Feb 3 05:11:43 EST 2004


How do I make a variable available to code *inside* my class in such a way
that it is available to all defs in the class? In my example below f is not
accessible within the showVars method.

class myClass:
    f = [1,2,3]

    def showVars ( self ):
        print f # Error here as f does not exist

def main():
    x = myClass()
    print x.f
    x.showVars()

if __name__ == "__main__":
    main()


Thanks to anyone who can offer some guidance to a Python beginner!
Graham.






More information about the Python-list mailing list