In python, I want to make a class variable static so I can to use it from a different instance. is there any solution?
            Asked
            
        
        
            Active
            
        
            Viewed 136 times
        
    0
            
            
        - 
                    This appears to be a duplicate of http://stackoverflow.com/questions/68645/static-class-variables-in-python – Peter C Mar 07 '11 at 01:06
- 
                    @alpha: From the reply to my post, it appears that it's the exact opposite question :). – Chinmay Kanchi Mar 07 '11 at 01:08
1 Answers
4
            I don't follow your question exactly, but it seems to me you're asking how to make instance variables in Python. The answer is to set them inside a method, preferably __init__() using the self reference.
class Foo(object):
    classVar = 0 #this is a class variable. It is shared between all instances
    def __init__(self, instanceVar):
        self.someVar = instanceVar
obj1 = Foo(10)
obj2 = Foo(42)
print obj1.classVar # prints 0
print obj2.classVar # prints 0
print obj1.someVar #prints 10
print obj2.someVar #prints 42
 
    
    
        Chinmay Kanchi
        
- 62,729
- 22
- 87
- 114
 
    