我必须得到一个类以外的两个变量的值,但我没有任何运气。你知道吗
我有这样的想法:
class AttentionDataPoint(DataPoint):
def __init__(self, _dataValueBytes):
DataPoint.__init__(self, _dataValueBytes)
self.attentionValue = self._dataValueBytes[0]
def __str__(self):
return "Attention Level: " + str(self.attentionValue)
class MeditationDataPoint(DataPoint):
def __init__(self, _dataValueBytes):
DataPoint.__init__(self, _dataValueBytes)
self.meditationValue = self._dataValueBytes[0]
我试过:
Att = AttentionDataPoint()
Value = Att.__init__(attentionValue)
对于另一个相同的变量也是如此,但是python要求我提供一个缺少的位置参数:_dataValueBytes
,但是我真的无法解决这个问题。你知道吗
数据点类是:
Class DataPoint:
def __init__(self, dataValueBytes):
self._dataValueBytes = dataValueBytes
假设
attentionValue
是要传递给__init__
的初始值,您可以这样调用它:当您这样调用类时,
self
会自动作为第一个位置参数传递。在第二个示例中,当直接调用__init__
时,只传递python分配给self
的一个参数,因此它会抱怨缺少_dataValueBytes
参数。你知道吗我不能说
data
应该是什么。无论您传递什么,都会映射到类内的_dataValueBytes
,因此该名称可能会给您一个它所期望的线索。你知道吗一旦您能够正确地构造对象,就可以非常简单地获取数据,因为它只是简单地查找属性:
当你写下:
在创建该类的实例时,如果查看该类的init函数,就会发现它需要一个参数才能初始化(DataValueBytes)。你知道吗
当您创建一个实例时,它会自动调用init函数,我认为这就是您的困惑所在(以后不需要显式调用它)。你知道吗
初始化该类后,要访问该变量,只需执行以下操作:
相关问题 更多 >
编程相关推荐