0
1. class Mine:
2.    def fun1(self,x):
3.        self.x=x
4.        print "Inside fun1",self.x

5.    def fun2(self):
6.        print "Inside fun2 :",self.x
7.
8.  obj1 = Mine()
9.  obj1.fun1(1)
10. obj1.fun2()

11. obj2 = Mine()
12. obj2.fun1(10000)
13. obj2.fun2()

14. del obj1.x
15. #obj1.fun2()
16. obj2.fun2()

17. Mine.a=111
18. obj3=Mine()
19. print "Mine.a: ",Mine.a
20. print "obj1.a: ",obj1.a
21. print "obj2.a: ",obj2.a
22. print "obj2.a: ",obj3.a

23. obj2.c=222
24. #print "\nMine.c: ",Mine.c
25. #print "obj1.c: ",obj1.c
26. print "obj2.c: ",obj2.c
27. #print "obj2.c: ",obj3.c

Can someone please help me to understand what is happening at line number 17 and 23, and why the code on line number 19,20,21,22 is working fine and code on line number 24, 25, 27 is giving error ?

Thanks in Advance.

0

1 Answer 1

1
17. Mine.a=111

you are adding a new static member a to the class Mine. the way python works (other object oriented languages do not do it this way) you can access a like it was a normal instance attribute; i.e. m = Mine(); print m.a will now work just as print Mine.a.

static members are normally added in the body of the class definition:

class Mine:
    a = 111

your second question:

23. obj2.c=222

this just adds a new member to the current instance obj2. neither the other insances of Mine nor the class Mine itself know about c.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.