python 类的实例对象传入另一个类的实例对象,内存地址相同吗

2023-01-10  本文已影响0人  Simple丶Plan

测试

import pandas as pd

class testA():
    
    def __init__(self, obj_frame, obj_list, obj_tuple):
        self.obj_frame = obj_frame
        self.obj_list = obj_list
        self.obj_tuple = obj_tuple
       
    def getid(self):
        print('testA, self.obj_frame, id:', id(self.obj_frame))
        print('testA, self.obj_list, id:', id(self.obj_list))
        print('testA, self.obj_tuple, id:', id(self.obj_tuple))
    
    def transfer(self):
        class_testb = testB(self.obj_frame, self.obj_list, self.obj_tuple)
        class_testb.getid()
    
    
class testB():
    
    def __init__(self, obj_frame, obj_list, obj_tuple):
        
        self.obj_frame = obj_frame
        self.obj_list = obj_list
        self.obj_tuple = obj_tuple
        
    def getid(self):
        print('testB, self.obj_frame, id:', id(self.obj_frame))
        print('testB, self.obj_list, id:', id(self.obj_list))
        print('testB, self.obj_tuple, id:', id(self.obj_tuple))


frame = pd.DataFrame([1,2,3,4], columns=['num'])
lst = [100]
tup = (0, 20)

ta = testA(frame, lst, tup)

ta.getid() 

'''
testA, self.obj_frame, id: 231173832
testA, self.obj_list, id: 231234248
testA, self.obj_tuple, id: 228010120
'''

ta.transfer()

'''
testB, self.obj_frame, id: 231173832
testB, self.obj_list, id: 231234248
testB, self.obj_tuple, id: 228010120
'''

结论:相互传递的两个类的实例对象内存地址不变。

上一篇 下一篇

猜你喜欢

热点阅读