使用for循環,我想追加字典的一個對象的屬性:訪問對象屬性的同一屬性循環的結果[python]的
do = direct_objects[0].point_logs
for direct_object in direct_objects: #Contains two objects
print (do is direct_object.point_logs) #Gives true both times
do = direct_object.point_logs
print(direct_object.id) #objects are different instances
dates = generate_dates_interval('2017-01-01', '2017-04-01', 14) #Generates dates with an interval of 14 days for the given period
for i in range(0, len(dates)-1):
point_logs, interval_logs, cum_logs = get_point_logs(direct_object, dates[i],
dates[i+1]) # gets logs for given period
if point_logs is not None:
direct_object.point_logs.append(point_logs)
if interval_logs is not None:
direct_object.interval_logs.append(interval_logs)
if cum_logs is not None:
direct_object.cum_logs.append(cum_logs)
我想通過日期循環和追加所有記錄到DirectObject的實例point_logs
。然而,我得到的是,不是兩個獨立的列表,direct_object.point_logs成爲兩個直接對象的所有點日誌的大列表。
class DirectObject(object):
def __init__(self, id, created_date, deleted_date, name, type, point_logs = [], interval_logs = [], cum_logs= [],
services = None):
self.id = id
self.created_date = created_date
self.deleted_date = deleted_date
self.name = name
self.type = type
self.point_logs = point_logs
self.interval_logs = interval_logs
self.cum_logs = cum_logs
self.services = services
def __str__(self):
return str(self.name) + " id: " + str(self.id) + " type: " + str(self.type)
def __repr__(self):
return str(self.name) + " id: " + str(self.id) + " type: " + str(self.type)
這是怎麼發生的?
這是雖然我只使用它用於調試目的 – k88
'do'不是一個Python關鍵詞BTW。它是一個'c'關鍵字。神祕的是爲什麼它像一個關鍵字突出顯示。 –
語法突出顯示了我:P – alDiablo