You will need some reference to the timestamp in the Data object itself. There is no implicit "ownership" between objects. They are only linked together because you explicitly do so (typically by giving one object a reference to another). Here are two methods:
First, by adding a reference to the Log object...
class Data(object):
def __init__(self, log, index):
self.log = log
self.index = index
self.val = random.random()
@property
def timestamp(self):
return self.log.timestamp
class Log(object):
def __init__(self):
self.timestamp = '09:50'
self.data = []
for i in range(0, 20, 5):
self.data.append(Data(self, i))
def get_timestamp_from_data(data):
return data.timestamp
...and another, which copies the timestamp to the Data object...
class Data(object):
def __init__(self, index, timestamp):
self.index = index
self.timestamp = timestamp
self.val = random.random()
class Log(object):
def __init__(self):
self.timestamp = '09:50'
self.data = []
for i in range(0, 20, 5):
self.data.append(Data(self.timestamp, i))
def get_timestamp_from_data(data):
return data.timestamp
Which you use (and I'm sure there are other methods) are up to you. Note that if for any reason the timestamp changes in Log, the first example will show that the timestamp changes in data, but in the second example, it will not...
log = Log()
cur_data = log.data[2]
print(log.timestamp) # Prints '09:50'
log.timestamp = '10:00'
print(log.timestamp) # Prints '10:00'
print(cur_data.timestamp)
This last line will print "10:00" if you use the first sample I've provided, since the data is retrieved from the reference from log. If you implement using the second way, you'll print "9:50", since the timestamp was "copied" from the log object when it was created. In this case, the Data object will contain a copy of its own timestamp.