namespace question

David dwblas at gmail.com
Fri Feb 24 19:08:43 CET 2012


Your code updated to show the difference between a variable, a class
variable, and an instance variable.
c = [1, 2, 3, 4, 5]
class TEST():
    c = [5, 2, 3, 4, 5]      ## class variable (TEST.c)
    def __init__(self):
        self.c = [1, 2, 3, 4, 5] ## instance variable (a.c)

    def add(self, c):
        self.c[0] = 15       ## instance variable
        TEST.c[0] = -1       ## class variable
        c[0] = 100           ## variable/list
        return c

a = TEST()
c = a.add(c)
print( c, a.c, TEST.c )



More information about the Python-list mailing list