DS160123What will be the output of the following

Date:-28/02/2023
What will be the output of the following python code?
class change:
def init(self.x,y,z):
x = change(1,2,3)
y = getattr(x,‘a’)
setattr(x,‘a’)
print(x.a)

[a] 0 [b] error [c] 6 [d] 7

2 Likes

Answer is error because there is syntax error in this code because code is incomplete.
The correct code can be like this:
class Change:
def init(self, x, y, z):
self.x = x
self.y = y
self.z = z

x = Change(1, 2, 3)
y = getattr(x, ‘a’, ‘attribute not found’)
setattr(x, ‘a’, 7)
print(x.a)

Then will we get output as 7

1 Like

Thanks @sms18680 learnt something new today

1 Like

@kharshavardhan31
Keep learning :heart:
Keep it up :slightly_smiling_face: