Is it possible to have static class variables or methods in python? What syntax is required to do this?
Variables declared inside the class definition, but not inside a method are class or static variables:
As @Daniel points out, this creates a class-level "i" variable, but this is distinct from any instance-level "i" variable, so you could have
This is different from C++ and Java, but not so different from C#, where a static member can't be accessed using a reference to an instance. See what the Python tutorial has to say on the subject of classes and class objects. @Steve Johnson has already answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.
@beidy recommends classmethods over staticmethod, as the method then receives the class type as the first argument, but I'm still a little fuzzy on the advantages of this approach over staticmethod. If you are too, then it probably doesn't matter. |
|||||||||||||||
|
@Blair Conrad said static variables declared inside the class definition, but not inside a method are class or "static" variables:
There are a few gotcha's here. Carrying on from the example above:
Notice how the instance variable 't.i' got out of sync with the "static" class variable when the attribute 'i' was set directly on 't'. This is because 'i' was re-bound within the 't' namespace, which is distinct from the 'Test' namespace. If you want to change the value of a "static" variable, you must change it within the scope (or object) where it was originally defined. I put "static" in quotes because Python does not really have static variables in the sense that C++ and Java do. Although it doesn't say anything specific about static variables or methods, the Python tutorial has some relevant information on classes and class objects. @Steve Johnson also answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.
@beid also mentioned classmethod, which is similar to staticmethod. A classmethod's first argument is the class object. Example:
|
|||||
|
Yes. You can have class methods using the @classmethod decorator. Also, if I am interpreting your question correctly, you can define static fields by declaring them just after your class name, like so (with example):
|
|||||||
|
Personally I would use a classmethod whenever I needed a static method. Mainly because I get the class as an argument.
or use a decorator
For static properties.. Its time you look up some python definition.. variable can always change. There are two types of them mutable and immutable.. Also, there are class attributes and instance attributes.. Nothing really like static attributes in the sense of java & c++ Why use static method in pythonic sense, if it has no relation whatever to the class! If I were you, I'd either use classmethod or define the method independent from the class. |
||||
|
You could also enforce a class to be static using metaclass.
Then whenever by accident you try to initialize MyClass you'll get an StaticClassError. |
|||
|
One special thing to note about static properties & instance properties, shown in the example below:
This means before assigning the value to instance property, if we try to access the property thru' instance, the static value is used. Each property declared in python class always has a static slot in memory. |
|||
|
You can also add class variables to classes on the fly
And class instances can change class variables
|
||||
|
Static methods in python are called classmethods. Take a look at the following code
Notice that when we call the method myInstanceMethod we get an error, this is because it requires that method be called on an instance of this class. The method myStaticMethod is set as a classmethod using the decorator @classmethod. Just for kicks and giggles, we could call myInstanceMethod on the class by passing in an instance of the class, like so
|
||||
|
To avoid any potential confusion, I would like to contrast static variables and immutable objects. Some primitive object types like integers, floats, strings, and touples are immutable in Python. This means that the object that is referred to by a given name cannot change if it is of one of the aforementioned object types. The name can be reassigned to a different object, but the object itself may not be changed. Making a variable static takes this a step further by disallowing the variable name to point to any object but that to which it currently points. (Note: this is a general software concept and not specific to Python; please see others' posts for information about implementing statics in Python). |
|||
|
The best way i found is to use another class....
At the example above i made class named staticFlag, This Script Results: False False False False False True True True True True |
|||
|
When define some member variable outside any member method, the variable can be either static or non-static depending on how the variable is expressed. CLASSNAME.var is static variable INSTANCENAME.var is not static variable. self.var inside class is not static variable. var inside the class member function is not defined.
The results are
|
|||
|