Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách truy cập Python objects bên trong objects trong Python?

Object bên trong object truy cập như sau:

Ví dụ

class P:
    def __init__(self):
        self.w = Q()
class Q:
    def __init__(self):
        self.list = [3, 4, 5]
    def function(self):
        self.list[2] = 7

y = P()
f = [y]
print(f[0].w.function())
print(f[0].w.list)

Output

None
[3, 4, 7]