What is the output of the following? class A: x = 5 a1 = A() a2 = A() a1.x = 10 print(A.x, a1.x, a2.x)

Python Developer Medium

Python Developer — Medium

What is the output of the following? class A: x = 5 a1 = A() a2 = A() a1.x = 10 print(A.x, a1.x, a2.x)

Key points

  • The class attribute x remains 5 for all instances of A
  • Changing a1.x to 10 only affects that specific instance
  • a2.x remains 5 since it was not modified

Ready to go further?

Related questions