Question number 8

What will be the answer to Q8? Can you please explain?

hey @Mohit2000 ,
This is actually a bit simple.
When we are doing [ A() ]*count , then it makes copies of same class object multiple times,
So if you check printing it then you will see that , it shows same memory address.

hence , when we do , b[2].x = 5 , as it refers to same memory address , hence we get a stream of same values on printing it.

Now , how can we correct it.
To correct we just change this above creation of list from
b= [A()]*10
to
b = [A() for i in range(10)]

in this way we are creating different object ( means every object has different memory location ) every time we iterate to create it.

I hope this helped you :slightly_smiling_face:.

Thank You and Happy Learning :slightly_smiling_face:.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.