Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类的python元素复制了新实例_Python_Attributes_Copy_Instance - Fatal编程技术网

类的python元素复制了新实例

类的python元素复制了新实例,python,attributes,copy,instance,Python,Attributes,Copy,Instance,我有一个名为KSlamComp的类,它从文件中读取数字并将其放入两个属性gt_raw和slam_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

我有一个名为
KSlamComp
的类,它从文件中读取数字并将其放入两个属性
gt_raw
slam_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")
数据
就是这样的东西

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_raw
self.gt_raw
是空的
数据
对象,列表为空。当我运行这个程序时,我有

$ 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_raw
Self.slam_raw
在两个对象中是同一个对象?如果我通过调用
d_invert=kslamcomp.kslamcomp(0,1,data.data(),data.data())
来“手动”初始化它们,这似乎是可行的,但我认为拥有默认参数是相同的。

不应该将可变对象用作函数的默认值,因为默认值存储在函数对象中。 我可能会写信

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

您不应该将可变对象用作函数的默认值,因为默认值存储在函数对象中。 我可能会写信

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

著名的诀窍:使用可变数据结构作为默认值著名的诀窍:使用可变数据结构作为默认值