Python Basics--local variables and global variables

Source: Internet
Author: User

Let's start with two graphs, the difference between a local variable and a global one:

Local variables:

  

class MyClass ():     def A (self):        n=100        Print (The N value in ' A is:%d'%(n))     def B (self):        n=n+200        Print (The n value in ' B is:%d'% (n))

This time will be an error, n=n+200 in function B will show n undefined error, because n is only defined in function a a value of 100, but the function B is not assigned a value, if you want to use the value of N, you can define a global variable, representing the distinction I write here _n

Global variables:

_n=1111classMyClass ():defA (self): n=100Print('the n value in a is:%d'%(n))defB (self):Global_n N=_n+1111Print('the n value in B is:%d'%(n))return_nret=MyClass ()Print(Ret.b ())

Results of the operation:

The n value in B is: 2222
1111

If you modify n=_n+1111 to _n=_n+1111, the global variable will change as well, as the sample code is:

_n=1111classMyClass ():defA (self): n=100Print('the n value in a is:%d'%(n))return_ndefB (self):Global_n _n=_n+1111Print('the n value in B is:%d'%(_n))return_nret=MyClass ()Print(ret.b ())Print(RET.A ())

Operation Result:

The n value in B is: 2222
2222
The n value in a is: 100
2222

As can be seen here, after the invocation of the B function, the global variable _n has been modified to become 2222, and then the next call to a function, the return value returned _n is already modified by the B function of the global variable _n, the value is 2222.

If you call the B function again, the value will change to 3333, because the global variable _n on the basis of 2222 again by the B function +1111.

The code is as follows:

_n=1111classMyClass ():defA (self): n=100Print('the n value in a is:%d'%(n))return_ndefB (self):Global_n _n=_n+1111Print('the n value in B is:%d'%(_n))return_nret=MyClass ()Print(ret.b ())Print(ret.b ())Print(ret.b ())Print(RET.A ())

Operation Result:

The n value in B is: 2222
2222
The n value in B is: 3333
3333
The n value in B is: 4444
4444
The n value in a is: 100
4444

Of course, if you look at the print results followed by the return value of the function also printed very uncomfortable, can also be directly written ret.b (), do not need print, this will only print: B in the number of N is: 2222, and will not return the value of 2222 after printing out

PS: It is worth noting that the global variables reserved word global generally do not omit to write, otherwise it will sometimes produce abnormal results.

Python Basics--local variables and global variables

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.