2017-05-04 167 views
0

我有類名爲KSlamComp,從文件中讀取數字,並把它在兩個屬性gt_rawslam_raw蟒蛇元素

class KSlamComp: 
    def __init__(self, forward_nodes_lookup = 1, backward_nodes_lookup = 0, slam_input_raw = data.Data(), gt_input_raw = data.Data()): 
     self.slam_raw = slam_input_raw 
     self.gt_raw = gt_input_raw 
     self.nb_node_forward = forward_nodes_lookup 
     self.nb_node_backward = backward_nodes_lookup 

    def read(self, file_name): 
     assert(len(self.slam_raw.posetime) == 0) 
     f = open(file_name, 'r') 
     for line in f: 
      print("line") 
      assert len(line.split()) == 8 

      slampose = data.Pose(data.Point(float(line.split()[0]), float(line.split()[1])), float(line.split()[2])) 
      gtpose = data.Pose(data.Point(float(line.split()[4]), float(line.split()[5])), float(line.split()[6])) 

      self.slam_raw.posetime.append((slampose, float(line.split()[3]))) 
      self.gt_raw.posetime.append((gtpose, float(line.split()[7]))) 


    def printraw(self): 
     print("Printing Raw data") 
     for x in range(0, len(self.slam_raw.posetime)): 
      print(str(self.slam_raw.posetime[x][0].getPosition().x) + " " \ 
       + str(self.slam_raw.posetime[x][0].getPosition().y) + " " \ 
       + str(self.slam_raw.posetime[x][0].getOrientation()) + " " \ 
       + str(self.slam_raw.posetime[x][1]) + " " + \ 
        str(self.gt_raw.posetime[x][0].getPosition().x) + " " \ 
       + str(self.gt_raw.posetime[x][0].getPosition().y) + " " \ 
       + str(self.gt_raw.posetime[x][0].getOrientation()) + " " \ 
       + str(self.gt_raw.posetime[x][1])) 
     print("\n") 

Data簡直是沿着這些線路的東西

class Data: 
    def __init__(self): 
     #Tuple with pose and time 
     self.posetime = list() 

現在我有這個測試文件

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 

from kslamcomp import data 
from kslamcomp import kslamcomp 

def main(): 
    # parse command line options 
    d = kslamcomp.KSlamComp(1, 0) 
    d.read("data_files/shifted.txt") 
    d.printraw() 
    d.print() 

    d_invert = kslamcomp.KSlamComp() 
    d_invert.printraw() 
    d.printraw() 

if __name__ == "__main__": 
    main() 

我的理解是,d_invert是一個新對象KSlamComp,其所有屬性都初始化爲默認值。特別是,self.slam_rawself.gt_raw是空的Data具有空列表的對象。 Hoever當我運行這個程序,我有

$ python3 test_sanity.py 
line 
line 
line 
line 
Printing Raw data 
1.0 1.0 1.0 2.0 3.0 3.0 3.0 6.0 
5.0 5.0 5.0 6.0 8.0 8.0 7.1 14.0 
9.0 9.0 9.0 10.0 11.0 11.0 11.0 2.0 
13.0 13.0 13.0 14.0 15.0 15.0 15.0 10.0 


Printing Raw data 
1.0 1.0 1.0 2.0 3.0 3.0 3.0 6.0 
5.0 5.0 5.0 6.0 8.0 8.0 7.1 14.0 
9.0 9.0 9.0 10.0 11.0 11.0 11.0 2.0 
13.0 13.0 13.0 14.0 15.0 15.0 15.0 10.0 


Printing Raw data 
1.0 1.0 1.0 2.0 3.0 3.0 3.0 6.0 
5.0 5.0 5.0 6.0 8.0 8.0 7.1 14.0 
9.0 9.0 9.0 10.0 11.0 11.0 11.0 2.0 
13.0 13.0 13.0 14.0 15.0 15.0 15.0 10.0 

雖然我認爲第二打印對子級已經空了,似乎包含在第一KSlamComp對象讀取數據。

爲什麼slef.gt_rawSelf.slam_raw兩個對象中的對象是同一個對象?如果我通過調用d_invert = kslamcomp.KSlamComp(0, 1, data.Data(), data.Data())「手動」初始化它們,它似乎可行,但我認爲具有默認參數是相同的。

+1

一個着名的疑難雜症:使用可變數據結構作爲默認值 – Maresh

回答

2

您不應該使用可變對象作爲函數的默認值,因爲默認值存儲在函數對象中。 我可能會寫

class KSlamComp: 
    def __init__(..., slam_input_raw = None, gt_input_raw = None, ...) 
     self.slam_raw = slam_input_raw or data.Data() 
     self.gt_stuff = gt_input_raw or data.Data()