Is it possible to have static class variables or methods in python? What syntax is required to do this?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
It is possible to have
static
class variables, but probably not worth the effort.Here's a proof-of-concept written in Python 3 -- if any of the exact details are wrong the code can be tweaked to match just about whatever you mean by a
static variable
:and in use:
and some tests:
@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 attributei
was set directly ont
. This is becausei
was re-bound within thet
namespace, which is distinct from theTest
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:
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).
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.
In regards to this answer, for a constant static variable, you can use a descriptor. Here's an example:
resulting in ...
You can always raise an exception if quietly ignoring setting value (
pass
above) is not your thing. If you're looking for a C++, Java style static class variable:Have a look at this answer and the official docs HOWTO for more information about descriptors.
Absolutely Yes, Python by itself don't have any static data member explicitly, but We can have by doing so
output
explanation