-3

Many had explained the use of static in Python but one point seems not got emphasized. In several other major languages static also means a static datum is a unique(only) copy in the scope. I am coding some astronomical objects that use large VSOP87 data sets. However all the class/derived class or the instances of classes will use only a particular set of the data. In C++/Java/C# using static for those data can easily fulfill the purpose. For instance in C++/C#/Java, the code will almost looks like below:

public class Earth
{
.....
static earthVSOP87XYZ[][] = {.....}; // in a very large size
}

All the Earth's instances share the only copy of earthVSOP87XYZ and the data are well encapsulated. Is there any way in Python 3 to achieve the same result?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
K Lamb
  • 49
  • 2
  • 1
    Please show a code sample that illustrates your problem. Static class variables in Python behave in the same way as in other languages: they exist in the class's scope and are unique regardless of the number of instances. – BartoszKP Feb 03 '17 at 18:39
  • What do you mean by "static" in Python? – juanpa.arrivillaga Feb 03 '17 at 18:48
  • Thank u BartoszKP! I tested what you said as: class TwoDimList: _Val = [ [ i for i in range(6) ] for j in range(6)] def __init__(self) : for d1 in range(6): for d2 in range(6): self._Val[d1][d2] = random.randint(0, 9) def printVal(self) : print(self._Val) b1.printVal() b2.printVal() And both with the same number. It seems that b1 and b2 are using the same instance of _Val. – K Lamb Feb 03 '17 at 20:04

1 Answers1

0

Great thanks for BartoszKP pointed out that data set defined in Python class behaves like static in other language. I used the code below to verify this:

class TwoDimList:
    _Val = [ [ i for i in range(6) ] for j in range(6)]

    def __init__(self) :
        for d1 in range(6):
            for d2 in range(6):
                self._Val[d1][d2] = random.randint(0, 9)

    def printVal(self) :
        print(self._Val)

b1.printVal()
b2.printVal()
K Lamb
  • 49
  • 2