static? + some stuff

Daniel Schüle for_usenet2000 at yahoo.de
Tue Nov 4 13:22:46 EST 2003


[...]

> is "cnt" considered to be used as "static" or not
> what if i would write ...
> foo.cnt += 1    #static
> and later in one of member functions
> self.cnt = 1    #non static

after long thinking i came to the conclusion that .. from what KefX said ..
"assigning to it .. would smash 'static' for this instance of the class"
i would say ...

class some_class:
    cnt = 0
    def __init__(self):
        foo.cnt += 1
        self.my_id = foo.cnt
    def some_func(self):
        self.cnt = 100
    def print(self):
        print foo.cnt

x = some_class()
x.print()    #1
y = some_class()
y.print()    #2

x.some_func()        #<--- smashes cnt ONLY for x instance?!?!
x.print()    #100

z = some_class()    #..but not for the instances that might come
z.print()    #3

i haven't tested that, ALL OUTPUT assumed!!!

[...]






More information about the Python-list mailing list