About all variables and local variables scopes

Source: Internet
Author: User

1  def FF (): 2      A=13      sum=a+b4      print(sum)5  b=16   print(b)7  ff ()8  print('  outside the function B', b)

This code will work as expected. Because B is defined outside the function, it is a global variable. Sum=a+b Direct reference to the value of B

1 b=12def  ff ():3     b+=14     print(b) 5 6 Print (b) 7 FF () 8 Print (' B ' outside the function, b)

This code will error. Because the variable b inside the function is a new variable relative to the outside of the function, just the name is exactly the same. and the B in the function is quoted without first assigning a value, so it will be an error.

About all variables and local variables scopes

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.