-2

And please explain the output of the below code as to why I am getting the output as written in the respective comments section of the code snippet:

class person():
  pass


p=person
q=person
r=person()

p.no=1
print(p.no) #output : 1
print(q.no) #output : 1
print(r.no) #output : 1
q.no=2
print(p.no) #output : 2
print(r.no) #output : 2
r.no=3
print(r.no) #output : 3
print(p.no) #output : 2
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

2

person refers to the class itself, you could compare assigning like p.no = 1 with assigning a public static member.

person() instantiates a new object of the class person. Assigning like r.no = 3 is like assigning a member variable.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195