Python类从父级获取相同的数据

时间:2018-04-17 18:43:48

标签: python class inheritance

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()

结果:

data downloaded
1523989483.9526002
data downloaded
1523989483.9536002

您可以看到时间不同意味着数据不相同导致数据是时间序列数据。

为了获得相同的数据,如何解决?

另外,我知道以下是可能的解决方案,但根本不是:

class B(A):

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

假设我有C,D,E类......使用C类(B),D类(C),E类(D)类不是一个好主意......

3 个答案:

答案 0 :(得分:0)

正如Sadjad Anzabi Zadeh指出的那样,除非你编码,否则继承不会共享数据。

请记住这是我在Python标签下的第一篇文章。

您要做的是设置一次数据(时间),然后在需要时检索它。

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)

答案 1 :(得分:0)

每次调用get_data不是由于继承而得到不同数据的原因,是因为_download_data不存储数据,而是返回数据并将其丢弃。

您需要的是具有 classmethod 的类Data来下载和存储数据。这样,数据就存储在类中,因此对于每个实例都是相同的。

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

答案 2 :(得分:-1)

继承并不意味着拥有相同的数据!当您从A派生类Bdata时,只表示AB可以拥有data的所有属性和方法,但它不保持相同的内容。 您可以在dataA中使用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)
相关问题