Suppose we have such a py file:
def fun ():
print ' Dada '
return u ' welcome '
Class Base:
A = Fun ()
def __init__ (self,c):
self.b = U ' Hello '
Print SELF.A
Base1 = Base ()
(1) First, it is easy to see that a is a class variable, B is an instance variable, and C is a local variable.
(2) Explain how the memory is allocated when we execute the PY file.
- The Func method is first loaded back into memory "analogy Java, temporarily considered in the method area"
- Because the class has its own storage space, load the base class into memory while loading __init__ "temporarily think in the method area"
- Create a Base1 instance, allocate memory for it, and execute the __init__ method
(3) Explain the class variable, how the instance variable is called
- The above storage graph shows that a is called by the base and B is called by base1, so can base1 call a? There are two situations: (1) For example, print base1.a "The output is the value of a" "Reason: guess is first from the BASE1 store to find a, no words to the base area to find, and then found, and finally output." (2) base1.a = ' Nihao ', this is actually creating an instance variable A for the BASE1 instance, regardless of the previous class variable A.
- Therefore, when calling class variable A, both inside and outside the class, use BASE.A when invoking instance variable B, using base1.b
Python class variables, instance (member) variables, local variables summary