0
from time import time

class data(object):

    def get_data(self):
        return self._download_data()

    def _download_data(self):
        """ download data code """
        print("data downloaded")
        return time()

class A(data):

    def __init__(self):
        self.data = self.get_data()
        print(self.data)

class B(data):

    def __init__(self):
        self.data = self.get_data()
        print(self.data)

if __name__ == "__main__":
    a = A()
    b = B()

Result:

data downloaded
1523989483.9526002
data downloaded
1523989483.9536002

You can see the time is different that means the data is not same cause the data is time series data.

In order to get same data, how can be fix?

Also, I knew the following is possible solution but not really at all:

class B(A):

    def __init__(self):
        self.data = self.get_data()
        print(self.data)

Lets say I have class C, D, E... it is not good idea of using class C(B), class D(C), class E(D)...

  • If your data changes with time and you need multiple objects to use the *same* data then you should only get it once and assign it to an attribute that all the objects can access - maybe use a module level function to get the data and assign the return value to a module level variable. – wwii Apr 17 '18 at 18:58
  • https://stackoverflow.com/a/6255101/2823755 – wwii Apr 17 '18 at 19:01
  • Using "__shared_state" is the best solution. Thank you! – onon99buynoodle Apr 17 '18 at 21:09

3 Answers3

0

The reason you get different data every time your call get_data is not due to inheritance, it's because _download_data does not store the data, it returns it and throws it away.

What you need is the class Data to have a classmethod that downloads and stores the data. This way, the data is stored in the class and so is the same for every instance.

from time import time

class Data:
    data = None

    @classmethod
    def download_data(cls):
        cls.data = time()
        print('Downloaded:', cls.data)

class A(Data):
    def get_data(self):
        print('A:', self.data)

class B(Data):
    def get_data(self):
        print('B:', self.data)

if __name__ == "__main__":
    a = A()
    b = B()

    a.get_data() # prints: None
    b.get_data() # prints: None

    Data.download_data() # prints: Downloaded: 1523995119.6320755

    a.get_data() # prints: A: 1523995119.6320755
    b.get_data() # prints: B: 1523995119.6320755
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
-1

Inheritance doesn't mean having the same data! When you derive classes A and B from data, it only means that A and B can have all the attributes and methods of data, but it doesn't keep the same content. You might be able to use data as an attribute in A and B:

from time import time

class data(object):

    def get_data(self):
        return self._download_data()

    def _download_data(self):
        """ download data code """
        print("data downloaded")
        return time()

class A:

    def __init__(self, d):
        self.data = d.get_data()
        print(self.data)

class B:

    def __init__(self, d):
        self.data = d.get_data()
        print(self.data)

if __name__ == "__main__":
    D = data()
    a = A(D)
    b = B(D)
-1

As Sadjad Anzabi Zadeh pointed out, inheritance doesn't share data unless you code it to do so.

Please bear in mind this is my first post under the Python tag.

What you want to do is set the data (time) once and then retrieve it whenever you need it.

from time import time

class data(object):

    def __init__(self):
        """ Call time() once and store it in self.data """
        self.data = self._download_data()

    def get_data(self):
        """ Just return the property without looking at time() again """
        return self.data

    def _download_data(self):
        return time()

class A:

    def __init__(self, d):
        """ Use d.data property """
        self.data = d.get_data()
        print(self.data)

class B:

    def __init__(self, d):
        """ Use d.data property """
        self.data = d.get_data()
        print(self.data)

if __name__ == "__main__":
    D = data()
    """ pass D to other classes and use its data property directly."""
    a = A(D)
    b = B(D)
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • For the same reason as the other given asnwer: This does not work, you cannot call D.get_data() since it is an instance method, not a class method. – Olivier Melançon Apr 17 '18 at 20:03