Computer >> Computer tutorials >  >> Programming >> Python

How to access Python objects within objects in Python?


The objects within objects can be accessed as follows

Example

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

Gives output as

None
[3, 4, 7]