Variables common to all objects of a class

Emile van Sebille emile at fenx.com
Fri Apr 12 11:07:27 EDT 2002


Thomas Guettler
> Hi!
> 
> How do you store variables which should be common to all
> objects of a class?
> 

class Foo:
    class_var_count_foos = 0 # shared by all instances
    def __init__(self, var):
        self.instance_var = var
        Foo.class_var_count_foos += 1

foo1 = Foo(1)
foo2 = Foo(2)
foo3 = Foo(3)

print foo1.class_var_count_foos
print foo1.instance_var

print foo2.instance_var


--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list