Python class variables, instance (member) variables, local variables summary

Source: Internet
Author: User

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.
    1. The Func method is first loaded back into memory "analogy Java, temporarily considered in the method area"
    2. Because the class has its own storage space, load the base class into memory while loading __init__ "temporarily think in the method area"
    3. Create a Base1 instance, allocate memory for it, and execute the __init__ method

     

(3) Explain the class variable, how the instance variable is called

    1. 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.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.