How to access Python objects within objects in Python?

PythonServer Side ProgrammingProgramming

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]
raja
Published on 24-Jan-2018 11:06:53
Advertisements