I need a way to create a static class variable in Python that only the class will have. I can't afford this variable to be created over and over again for each instance, because the target class variable will be a huge read-only data structure. Each instance must be able to access it for read-only purposes throught the class, but since it's a giant bulk of data and there will be thousands instances, I can't afford that each instance will create another copy of this data structure again.
I found this question, but it doesn't solve my problem, because the static class variable suggested is created for each instance:
>>> class MyClass():
i = 1
>>> m = MyClass()
>>> m.i
1
In other words, I'm looking for a static class variable like in PHP:
<?php
class Foo
{
public static $my_static = 'foo';
}
$foo = new Foo();
print $foo::$my_static . "\n"; // Accessable throught the class
print $foo->my_static . "\n"; // Undefined "Property" my_static
?>
This is exactly what I need, the static class variable is created only once for the class, all instances can access it throught the class but this static class variable isn't created over and over for each new instance. Is it possible to create a static class variable like this in Python?
PS: I now there are some workarounds if I don't use OOP, but if I could find a good and clearly readable OOP solution it would make more sense in the project as a whole.