[Tutor] Object attributes surviving deletion

ALAN GAULD alan.gauld at btinternet.com
Fri Jun 27 07:40:06 CEST 2008


> Though I solved the problem by making database an instance variable,
> there's one thing I'm curious about. If I 'overwrite' a class variable
> with an instance one (as I did originally), is the class variable
> recoverable? 

Yes, you can always access the class version by using the class 
as the accessor

class C:
     cv = 42   # class variable

c = C()
c.cv = 666   # create instance cv in c
c2 = C()

print c.cv  # -> 666
print C.cv   # -> 42
print c2.cv  # -> 42, new instance so no instance variable exists


> Will objects created later have the class or the instance
> variable?

The class one. The instance variable you create is purely 
in that one instance. You do not alter the class definition 
so new instamces do not pick it up.

HTH,

Alan G.



More information about the Tutor mailing list